Thanks, here is the result:feyd wrote:Pull the whole
- in as a string and parse it separately then replace as needed.
Code: Select all
<?php
function bbcode($string)
{
$bbcode = array(
'/\[br\]/s',
'/\[b\](.*?)\[\/b\]/s',
'/\[i\](.*?)\[\/i\]/s',
'/\[url=(.*?)\](.*?)\[\/url\]/s');
$html = array(
'<br>',
'<strong>$1</strong>',
'<em>$1</em>',
'<a href="$1">$2</a>');
$string = htmlentities($string);
$string = preg_replace($bbcode, $html, $string);
$string = preg_replace_callback('/\[list\](.*?)\[\/list\]/s', 'bblist', $string);
return $string;
}
function bblist($maches)
{
$list = '';
$list_array = explode('[*]', $maches[1]);
foreach ($list_array as $key => $value)
{
if ($key == 0) $list .= $value;
else $list .= '<li>' . $value . '</li>';
}
return '<ul>' . $list . '</ul>';
}
echo bbcode('
[list]
no li here
[*]one
[*]two[br]
new line without li
[*]close[/list][/list]
');
?>