A-OK
A-OK

Reputation: 3254

preg_replace help - need to replace [list]..[/list] with HTML

I can't get my head around this and I could really use some help.

I want to replace text like this

[list]line 0
line 1
line 2
[/list]

with

<ul><li>line 0</li>
<li>line 1</li>
<li>line 2</li>
</ul>

It can appear several times in the string and it would be great if it can avoid adding empty list elements.

Upvotes: 0

Views: 273

Answers (2)

Danzan
Danzan

Reputation: 968

preg_replace('#[list](.*?)[/list]', $data, $list);
$list[1] = explode("\n", $list);
$html = '<ul>';

foreach($list[1] as $li){
   $html .= '<li>'.$li.'</li>';
}
$html .= '</ul>';

Upvotes: 1

Craig White
Craig White

Reputation: 14012

Here is my special BBParser. Spent ages on it.

http://pastebin.com/3MRYRd8k

Use it like this:

$parse = new bbParse();
$pageContent = $parse->bbtohtml($dbContent);

Upvotes: 3

Related Questions