Page 1 of 1

[link]...[/link] BBcode parsing

Posted: Wed Jan 12, 2005 1:47 pm
by sirfragalot
Hi all,

I have the following:

// plain list
$patterns = array("/\[list\]/i", "/\[\*\]([^\[.]+)/i", "/\[\/list\]/i");
$replacements = array("<ul>", "<li>\\1</li>", "</ul>");
$string = preg_replace($patterns, $replacements, $string);
// fancy list
$patterns = array("/\[list=(a|A|i|I|1)\]/i", "/\[\*\]([^\[.]+)/i", "/\[\/list\]/i");
$replacements = array("<ol type=\"$1\">", "<li>$2</li>", "</ol>");
$string = preg_replace($patterns, $replacements, $string);

When I enter:
[list=A]
[*]1
[*]2
[/list]


The list is displayed correctly but, any text underneath the list is indented towards the right as if it were a list within the first list.

This seems to be because my HTML source code is:
<ol type="A">
<li>1
</li><li>2
</li></ul>


What should be a closing </ol> is a closing </ul>

If I reverse the parsing of plain lists and fancy lists in the code, I no longer get the text following indented, but my list is empty with the source code being:
<ol type="A">
<li></li><li></li></ol>


I'm at a loss - any help?

Posted: Wed Jan 12, 2005 2:05 pm
by feyd
phpBB, as well as my own version of it figures out the nesting orders, because each type of list can appear in the other. Therefore, you need to use a preg_match_all() with the PREG_CAPTURE_OFFSET flag set so your script can know the nesting orders of them.