-6

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.

4

3 に答える 3

3

最も簡単な解決策は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));

?>

テキストを単一の単語に分割するからの正規表現

于 2013-10-04T14:32:36.567 に答える