你能告诉我如何显示一个包含20个单词的字符串的前10个单词吗?我有一个可以很好处理非UTF8字符的函数,但如何处理UTF8字符呢?
$parts = preg_split('/(\p{L}+)/u', $str, -1, PREG_SPLIT_DELIM_CAPTURE);
$excerpt = implode('', array_slice($parts, 0, 20));
像这样的东西
explode() - 通过字符串分割另一个字符串
会对你有帮助吗?
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;
或者你可以使用
strtok - 字符串分割函数