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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sirfragalot
Forum Newbie
Posts: 14
Joined: Wed Jan 12, 2005 1:42 pm

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

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
Post Reply