Jump to content

BBCode list with newlines


Dragen

Recommended Posts

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!!

Link to comment
Share on other sites

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);
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.