从字符串返回前x个段落

Dan*_*Dan 0 php paragraphs

如何使用PHP从字符串返回前x个段落?它们由\ r \n分隔,但<p></p>如果需要可以放入标签中.

Yac*_*oby 11

//split $s into paragraphs
$a = explode("\r\n", $s);

//extract the first $x paragraphs only
$a = array_slice($a, 0, $x);

//rejoin the paragraphs into a single string again
$s = implode('\r\n', $a);
Run Code Online (Sandbox Code Playgroud)