Re: RegExp for Forum-List-Code
| From: | Ernest E Vogelsinger | Date: | Sat, 09 Nov 2002 19:50:50 +0000 |
| Subject: | Re: RegExp for Forum-List-Code | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-123614@lists.php.net to get a copy of this message | ||
At 17:19 09.11.2002, Jens Lehmann said:
--------------------[snip]--------------------
>Hi out there,
>
>I encountered a lot of problems while trying to convert a list
>in Forum-Code (like UBB-Code).
>
>[list]
>[*] item 1
>[*] item 2
>[*] item 3
>[/list]
>
>should be converted to
>
><ul>
><li>item 1</li>
><li>item 2</li>
><li>item 3</li>
></ul>
--------------------[snip]--------------------
Maybe you could use this solution. It supports newlines and special
characters in the item's data as well:
<?php
function ubb2ul($src)
{
// retrieve the items
preg_match('/\[list\]\n?(.*?)\n?\[\/list\]/is', $src, $ar);
// now split the results by (opt) newline, followed by '[*]', (opt)
followed by whitespace
$ar = preg_split('/\n*\s*\[\*\]\s*/s', $ar[1]);
// $ar[0] is empty since the first element begins with a [*], chop
it off
array_shift($ar);
// apply htmlentities
for ($i = 0, $c = count($ar); $i < $c; ++$i)
$ar[$i] = nl2br(htmlentities($ar[$i]));
// finally construct the list
$result = '<ul><li>' . join("\n\t<li>", $ar) .
"\n</ul>\n";
return $result;
}
$ubb = '[list]
[*] item 1 äöü line 1
item 1 line 2
[*] item 2
[*] item 3
[/list]';
echo ubb2ul($ubb);
?>
--
>O Ernest E. Vogelsinger
(\) ICQ #13394035
^ http://www.vogelsinger.at/