开发者

Selecting 50 Words from a string

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜