• 0

[PHP] - preg_replace / Regex Question


Question

Im building a BBCode parser for my application, and this is driving me nuts.

How can I parse this BBCode using preg_replace.

[list]
[*]Item 1
[*]Item 2
[/list]

Every time I try, it ends up screwing the rest of the page. This is the code Im using to parse the

  • , but any suggestions are much welcomed:

array(#\[list](.*?)\[/list\]#si' => '<ul>\\1</ul>')

I need it to stop parsing after it reaches the end of the line for each [*], otherwise the whole page becomes a wonder list.

Link to comment
Share on other sites

3 answers to this question

Recommended Posts

  • 0

I guess the end delimiter will be \n, something along the lines of;

preg_replace('\[\*](.*)\\n/', '<li>\1</li>', $string);

Not tested, but hopefully you get where I was going with it (:

Link to comment
Share on other sites

  • 0

Have you looked at how other folk do it? RegExp wouldn't be my preferred solution, it falls down when you have nested tags.

Here's how PHPBB does it, link.

Check out this recent thread on SitePoint, we're discussing the use of a maintained, well constructed library for this task.

Link to comment
Share on other sites

  • 0

I haven't encountered any nested tag issues yet, but I haven't fully tested it as I am still setting up the whole parser. I'm trying to keep this pretty simple and lightweight, as It needs to be user editable right now, and the code is pretty easy to understand at this point. Just need the regex that will tell it to stop parsing at a newline.

Heres the function: http://pastebin.com/qU7ge5nK

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.