开发者

Selecting 50 Words from a string

开发者 https://www.devze.com 2023-02-19 05:59 出处:网络
I have a string that contains over 1200 chars. I want to on开发者_Python百科ly select the first 50 words.

I have a string that contains over 1200 chars. I want to on开发者_Python百科ly select the first 50 words.

the string is $row['message']. I am confused between explode and substr.

Thanks.


$words = preg_split('/\s+/', $row['message']);

$words = array_slice($words, 0, 50);

This code should do it.


Try

$words = str_word_count($row['message'], 1);
$first50Words = array_slice($words, 0, 50);


function firstNwords($str,$n){
    return preg_replace('/((\b\w+\b.*?){'.$n.'}).*$/s','$1',$str);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消