H
H
Hy_ok2018-03-22 07:30:18
PHP
Hy_ok, 2018-03-22 07:30:18

Where can I get a lib for declension of words and bringing them to a number?

In general, the point is. that there is a problem, the generation of titles and descriptions, for SEO (complete nonsense, as for me, it’s good that they are manually driven in when creating a category and other things), so the generation is based on the categories of brands of subcategories, and in places you need to decline words and quote them to a different number (plural and singular), the first time I encountered this, can anyone tell me some kind of library?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
I
ice2038, 2018-03-22
@hy_ok

There are two great solutions Morphos and Morpher . The second is paid, but if you use it as a web service, then the free limit is enough for simple tasks.

S
Sergey Sokolov, 2018-03-22
@sergiks

If you just need to correctly decline words, depending on the number, then this is just a few lines (in different languages). In PHP:

/**
 * Функция возвращает окончание для множественного числа слова на основании числа и массива окончаний
 * param  $number Integer Число на основе которого нужно сформировать окончание
 * param  $endingsArray  Array Массив слов или окончаний для чисел (1, 4, 5),
 *         например array('яблоко', 'яблока', 'яблок')
 * return String
 */
function getNumEnding($number, $endingArray)
{
    $number = $number % 100;
    if ($number>=11 && $number<=19) {
        $ending=$endingArray[2];
    }
    else {
        $i = $number % 10;
        switch ($i)
        {
            case (1): $ending = $endingArray[0]; break;
            case (2):
            case (3):
            case (4): $ending = $endingArray[1]; break;
            default: $ending=$endingArray[2];
        }
    }
    return $ending;
}

I
Ilya, 2018-03-22
@u1tr0n

here is a good lib php_rutils can do a lot

V
Vitaly Rykovanov, 2018-03-22
@Zextremo_Vital

I use this function on my projects to put the right ending for a word depending on the number.
Example: 5000 chips => 5000 chips .
Code example in function comment.

/**
     * Вывод окончания слова в зависимости от значения числа
     * Пример: 5000 фишка => 5000 фишек
     * echo '5000 фиш'.Common::trueEnd(5000, 'ка', 'ки', 'ек');
     *
     * Как задать параметры: выводим начало слова, а окончание выведет функция
     * n1 - 1 фиш-ка
     * n2 - 2 фиш-ки
     * n5 - 5 фиш-ек
     * @param int $number
     * @param string $n1
     * @param string $n2
     * @param string $n5
     * @return string
     */
    public static function trueEnd($number, $n1, $n2, $n5)
    {
        $number = (is_array($number)) ? count($number) : (int) $number;
        $ch = (int) substr($number, -2);
        if ($ch > 4 and $ch < 21) return $n5;
        $ch = (int) substr($number, -1);
        if ($ch == 1) return $n1;
        return ($ch > 1 and $ch < 5) ? $n2: $n5;
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question