开发者

Return first x paragraphs from a string

开发者 https://www.devze.com 2022-12-13 17:49 出处:网络
How can I return the first x paragraphs from a string using PHP? They\'re seperated by \\r\\n but can be put into &开发者_运维问答lt;p></p> tags if needed.//split $s into paragraphs

How can I return the first x paragraphs from a string using PHP? They're seperated by \r\n but can be put into &开发者_运维问答lt;p></p> tags if needed.


//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);


If you're in an xml/html environment, and not plain text, you might consider an xml or DOM parser. I'm not a php guy, but this an example dom parser for php: http://simplehtmldom.sourceforge.net/

Generally much more flexible and powerful, with simple API, then doing the parsing yourself.

0

精彩评论

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

关注公众号