Thread: BB Code
View Single Post
Old 10-25-2007, 11:35 AM   #10 (permalink)
Salathe
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

The problem with bluesaga's function is that the patterns are fed into preg_quote which renders them useless for your needs.

An amended function could be something along the lines of:
PHP Code:
function BBcode($szInput)
{    
    
$aBBPattern = array(
        
'<strong>\\1</strong>'  => '#\[b\](.*?)\[/b\]#is',
        
'<em>\\1</em>'          => '#\[i\](.*?)\[/i\]#is',
        
'<u>\\1</u>'            => '#\[u\](.*?)\[/u\]#is',
        
'<img src="\\1" />'     => '#\[img\](.*?)\[/img\]#i',
        
'<a href="\\1">\\1</a>' => '#\[url\](.*?)\[/url\]#i',
        
'<a href="\\1">\\2</a>' => '#\[url=([^\]]+?)\](.*?)\[/url\]#i'
    
);
    return 
preg_replace(array_values($aBBPattern), array_keys($aBBPattern), $szInput);


Last edited by Salathe : 10-25-2007 at 12:11 PM.
Salathe is offline  
Reply With Quote