Dragen Posted March 16, 2009 Share Posted March 16, 2009 Hi, I'm currently using my own BBCode to create bullet point liists (<ul><li>). I've got two preg replace calls to turn the code into the HTML lists: (\[list\](<br />)*(.+?)\[/list\](<br />)*)s which finds the [list][/list] block and replaces it with <ul></ul> (\[\*\](.+?)(<br />)+) which finds [*] and the following text and creates the <li></li> For example, the following text: [list] [*]list item 1 [*]list item 2 [*]list item 3 [/list] would become: <ul> <li>list item 1</li> <li>list item 2</li> <li>list item 3</li> </ul> The problem I've got is that I need to be able to capture any newline characters within the BBCode [*] section. At the moment, if I try to add a newline in the list it doesn't create the <li></li> properly, it adds the </li> at the newline, when I only want the </li> added on the last newline character: [list] [*]item list 1 [/list] currently creates: <ul> <li>item</li> list 1 </ul> When it should be: <ul> <li>item<br /> list 1</li> </ul> I've got another part which changes all newlines to <br />, I just need the list part to ignore all, but the last newline in the [*] section. I need to somehow change this regex to do this: (\[\*\](.+?)(<br />)+) I've been working on it for ages and it's just evaded me, any help would be brilliant!! Quote Link to comment https://forums.phpfreaks.com/topic/149665-bbcode-list-with-newlines/ Share on other sites More sharing options...
sasa Posted March 16, 2009 Share Posted March 16, 2009 try <?php $test = '<ul> [*]list item 1 [*]list item 2 [*]list item 3 </ul>'; echo $out = preg_replace_callback('/\[\*\]([^[]*)\n(?=\[|<)/', create_function('$a', 'return "<li>".nl2br($a[1])."</li>\n";'), $test); ?> Quote Link to comment https://forums.phpfreaks.com/topic/149665-bbcode-list-with-newlines/#findComment-785910 Share on other sites More sharing options...
Dragen Posted March 16, 2009 Author Share Posted March 16, 2009 Thanks, but that doesn't work as I want to be able to use other bbcode within the lists, which my ereg allows. could you explain what this part of your regex is doing? (?=\[|<) Quote Link to comment https://forums.phpfreaks.com/topic/149665-bbcode-list-with-newlines/#findComment-785985 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.