Forum Moderators: coopster
Now to do this I need to either find the double line break within the [list] tags and convert it to </li><li> or find the </p><p>, finding these characters is obviously no problem but I am having trouble isolating them within the [list]...[/list] tags.
The markup will be similar to this:
[list]
item 1
item 2
item 3
[/list]
and should output
<ul>
<li>item 1</li>
<li>item l</li>
<li>item 1</li>
</ul>
Ok so the way to do this would be to have:
$text = preg_replace("/\[list\](.*?)\[\/list\]/s", "<ul>$1</ul>", $text);
This will create:
<ul>
item 1
item 2
item 3
</ul>
What I need is for someone to point me into the right direction for writing regex's like the following:
/\[list\].*\r\n.*\[\/list\]/s, "</li><li>" where only the \r\n (or \r\n\r\n as in the example it doesnt really matter) is replaced.
Thanks,
- Ryan
function dostuff($str) {
return "<ul>" . preg_replace(array("/^\s+/s", "/(.*?)(\r?\n)+/gs"), array("", "<li>$1</li>"), $str . "\r\n") . "</ul>";
}
$text = preg_replace("/\[list\](.*?)\[\/list\]/s", "dostuff", $text);
function convert($data)
{
$data = eregi_replace(quotemeta("[*ul]"), quotemeta("<ul>"), $data);
$data = eregi_replace(quotemeta("[*/ul]"), quotemeta("</ul>"), $data);
$data = eregi_replace(quotemeta("[*li]"), quotemeta("<li>"), $data);
$data = eregi_replace(quotemeta("[*/li]"), quotemeta("</li>"), $data);
return $data;
}
Note: stars added to prevent formatting
dc
For anyone else that may need this functionality it is important to return the 0 index of the matches array, for instance (this is not my code but a simplified version for illustration purposes):
function callBack($text) {
$text = str_replace("\r\n","</li><li>");
return $text[0]; //I return 0 as this is the complete match, $text[1] is the first match, etc.
}
echo preg_replace_callback("/\[list\](.*?)\[\/list\]/s", "callBack", $subject);
- Ryan