String Limit Words - UTF8

Can you explain to me how to display, say, the first 10 words of a line that contains 20 words. I have a function that works well with non-utf8 letters, but how to do this with utf8 letters?

+3
source share
2 answers

You can split your line into words and word separators, and then extract the first ten words from it:

$parts = preg_split('/(\p{L}+)/u', $str, -1, PREG_SPLIT_DELIM_CAPTURE);
$excerpt = implode('', array_slice($parts, 0, 20));
+3
source

There will be something like

explode () - Split string by string

to be your friend

http://www.php.net/manual/en/function.explode.php

$words = "My String that contains over ten words etc etc etc etc";
$wordArray = explode(' ', $words);

$summary = array();
for($i = 0; $i < 10; $i++){
   $summary[] = $wordArray[$i];
}

$summary = implode(' ', $summary);
echo $summary;

Or you can use

strtok - Tokenize string

http://uk3.php.net/manual/en/function.strtok.php

0
source

All Articles