I have a string that can contain up to 2000 chars. I want to only show the first 40 words.
The string is $row['content']
. How would I only show the first 50?
Thanks.
最も簡単な解決策はwordwrap()です。しかし、記号ではなく40/50語が必要なので、次のようにする必要があります。
<?php
$string = "Your long string";
$result = preg_split('/((^\p{P}+)|(\p{P}*\s+\p{P}*)|(\p{P}+$))/', $string, -1, PREG_SPLIT_NO_EMPTY);
$words = implode(' ', array_slice($result, 0 ,50));
?>
テキストを単一の単語に分割するからの正規表現