Preg_replace BBCode Link
1 answer
You need a regular expression. Whereas bbcode can have a text url or just a url, you need two operators:
$message = preg_replace('@\[url=([^]]*)\]([^[]*)\[/url\]@', '<a href="$1">$2</a>', $message);
$message = preg_replace('@\[url\]([^[]*)\[/url\]@', '<a href="$1">$1</a>', $message);
Also, if you are parsing bbcode from PHPBB, it can have a unique user id:
$uid = '[0-9a-z]{5,}';
$message = preg_replace('@\[url=([^]]*):'. $uid . '\]([^[]*)\[/url:'. $uid . '\]@', '<a href="$1">$2</a>', $message);
$message = preg_replace('@\[url:'. $uid . '\]([^[]*)\[/url:'. $uid . '\]@', '<a href="$1">$1</a>', $message);
+5
source to share