Forum Moderators: coopster

Message Too Old, No Replies

Using preg_replace to identify & format lists

         

buriedUnderGround

9:52 am on May 16, 2005 (gmt 0)

10+ Year Member



Can anyone suggest any ways to streamline the following. It seems a bit chunky to me. Basically I want php to identify a list (within $text) and re-format it into the appropriate HTML tags, ie:<ul><li>...</li><li>...</li></ul>

Also, is there any way to do this in a single preg_replace() instead of using two as i have below?

$text = "normal text blah blah blah
==list item.
==another item.
==test. test.
==something else.";

$text = nl2br($text);

$text = preg_replace('/(<br \/\>([\n\r]\s))((==[-a-zA-Z0-9( )_\.,\"\'?!\/&()+:;]+(<br \/\>([\n\r]\s)))+)/', "<ul>$3</ul>", $text);
$text = preg_replace('/(==)(.*)(<br \/>)/',"<li>$2</li>",$text);

buriedUnderGround

9:23 am on May 17, 2005 (gmt 0)

10+ Year Member



I've refined my regular expressions to the following which seem to work much better:

$text = preg_replace('/((==.+(\n)?)+)/', "<ul>$1</ul>", $text);
$text = preg_replace('/(==)([^<]*)/',"<li>$2</li>",$text);

I would still like to know if it is possible to do something like this using only 1 preg_replace() instead of doing it in 2 steps.