Declination of words in php \ javascript
Склонение слов на php\javascript

All English-language materials have been translated fully automatically using the Google service

Word declension in javascript

We will use a small javascript function for declension of numerical values. As arguments, it is passed a number and an array of word forms with different numerical values. The result can be used as intended.

  function num2str (n, text_forms) {
n = Math.abs (n)% 100; var n1 = n% 10;
if (n> 10 && n <20) {return text_forms [2]; }
if (n1> 1 && n1 <5) {return text_forms [1]; }
if (n1 == 1) {return text_forms [0]; }
return text_forms [2];

The function is applied as follows:

  $ ('# p1'). html ('1' + num2str (1, ['minute', 'minutes', 'minutes']));
$ ('# p2'). html ('2' + num2str (2, ['minute', 'minutes', 'minutes']));
$ ('# p3'). html ('5' + num2str (5, ['minute', 'minutes', 'minutes']));  

Word declension in php

  / *
 * $ num number, on which the form of the word will depend
 * $ form_for_1 the first form of a word, for example Product
 * $ form_for_2 second form of the word - Goods
 * $ form_for_5 third plural form - Goods
 * /
function true_wordform ($ num, $ form_for_1, $ form_for_2, $ form_for_5) {
$ num = abs ($ num)% 100; // take the number modulo and reset hundreds (divide by 100, and assign the remainder to the $ num variable)
$ num_x = $ num% 10; // reset tens and write to a new variable
if ($ num> 10 && $ num <20) // if the number belongs to the segment [11; 19]
return $ form_for_5;
if ($ num_x> 1 && $ num_x <5) // otherwise if the number ends in 2,3,4
return $ form_for_2;
if ($ num_x == 1) // otherwise if it ends in 1
return $ form_for_1;
return $ form_for_5;

Function application:

  $ max_product = 5; // number, this variable can be set through some other function or taken from the base - it doesn't matter
echo $ max_product. ''. true_wordform ($ max_product, 'product', 'product', 'products'); // the result will be "5 products"  

Materials used:


There are no comments yet, you can be the first to leave it

Leave a comment

The site uses a comment pre-moderation system, so your message will be published only after approval by the moderator

You are replying to a user's comment



Email me

Are you developing a new service, making improvements to the existing one and want to be better than your competitors? You have come to the right place. I offer you a comprehensive studio-level website development. From me you can order design, layout, programming, development of non-traditional functionality, implementation of communication between CMS, CRM and Data Analitics, as well as everything else related to sites, except for promotion.

Contact, I will always advise on all questions and help you find the most effective solution for your business. I am engaged in the creation of sites in Novosibirsk and in other regions of Russia, I also work with the CIS countries. You will be satisfied with our cooperation

An error occurred while sending, please try again after a while
Message sent successfully


+7(993) 007-18-96



Россия, г. Москва

By submitting the form, you automatically confirm that you have read and accept the Privacy Policy site

Contact with me
Send message
By submitting the form, you automatically confirm that you have read and accept Privacy policy of site
Sending successful!
Thank you for contacting :) I will contact you as soon as possible
Sending failed
An error occurred while sending the request. Please wait and try again after a while or call my phone number