parsing multiple lists in bbcode?

php, regex

Solution

Rather than using Regular Expressions you have a couple of options..

- Use PHP's BBCode Parsing extension

- Do a much simpler replacement, ie. straight up replace `[ul]` with `<ul>` etc.

I'm not saying it can't be done with Regex, just that it's not the simplest option.

Here's a still-regex based replacement:

$body = '[ul][li]test[/li][li]test[/li][li]test[ul][li]lol[/li][/ul][/li][li]hehe[/li][/ul]';

$find = array(
    '/\[(\/?)list\]/i',
    '/\[\*\](.*?)(\n|\r\n?)/i',
    '/\[(\/?)ul\]/i',
    '/\[(\/?)li\]/i'
);
$replace = array(
    '<$1ul>',
    '<li>$1</li>',
    '<$1ul>',
    '<$1li>'
);
$body = preg_replace($find, $replace, $body);

Problem

Hi all I have a very simple bbcode parsing system, it is currently having problems with lists within lists. My code: ``` $find = array( '/\[list\](.*?)\[\/list\]/is', '/\[\*\](.*?)(\n|\r\n?)/is', '/\[ul\](.*?)\[\/ul\]/is', '/\[li\](.*?)\[\/li\]/is' ); $replace = array( '<ul>$1</ul>', '<li>$1</li>', '<ul>$1</ul>', '<li>$1</li>' ); $body = preg_replace($find, $replace, $body); ``` The problem is when you have another list inside the li tags it then completely fails to parse, screenshot showing: This is how it should look: I know my code is probably too simple for it but how do i adjust it so it can parse a list within a list item?

Original source