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);
}
精彩评论