PHP: crop text at word boundaries

Someone has the exact name of the function that Drupal uses to flip the following line:

"Hello, how are you. Another text."

in

"Hello how..."

those. The function that was used to cut off the sentence after the words x, and then add elipsis. Alternatively, if someone has a php fragment that does this, this is great too!

+3
source share
5 answers
function getFirstWords($string, $words = 1)
{
    $string = explode(' ', $string);

    if (count($string) > $words)
    {
        return implode(' ', array_slice($string, 0, $words)) . '...';
    }

    return implode(' ', $string);
}

echo getFirstWords('Hello, how are you. Some more text.', 2); // Hello, how...
+6
source

It seems that truncate_utf8()in unicode.inc.

+6
source

, , . , Drupal, .

+1

views_trim_text ($ alter, $value)

more details https://api.drupal.org/api/views/views.module/function/views_trim_text/7

$alter['html'] = TRUE;
$alter['max_length'] = 200;
$alter['word_boundary'] = TRUE;
$alter['ellipsis'] = TRUE;
print views_trim_text($alter, $output);
+1
source

Source: https://habr.com/ru/post/1715050/


All Articles