Parsing multiple lists in bbcode?

Hi everyone I have a very simple bbcode parsing system, it currently has problems with lists in lists.

My code is:

$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 that you have another list inside the li tags, after which it is not completely parsed, the screenshot shows: image1

Here's how it should look: images2

I know that my code is probably too simple for him, but how to configure it so that it can parse a list in a list item?

+4
source share
1 answer

Instead of using regular expressions, you have several options.

I'm not saying that this cannot be done with Regex, it’s just not the easiest option.

Regex based replacement is used here:

 $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); 
+3
source

Source: https://habr.com/ru/post/1490458/


All Articles