How can I return the first x paragraphs from a string using PHP? They're seperated by \r\n but can be put into <p></p>
tags if needed.
views:
64answers:
2
+8
A:
//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);
Yacoby
2009-12-12 12:45:29
Works perfectly, thanks!
Dan
2009-12-12 13:08:09
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.
Richard
2009-12-12 12:56:15