View Single Post
Old 04-06-2008, 09:37 AM   #5 (permalink)
mortisimus
The Contributor
 
mortisimus's Avatar
 
Join Date: Sep 2007
Location: London, UK
Posts: 47
Thanks: 4
mortisimus is on a distinguished road
Default

For capitals, you will need to add an extra variable:

PHP Code:
$lower strtolower($input); 
And then set $lower as the input, so your new code would be:
PHP Code:
function bbcode($input){
    
//lower the string to non-capitals
    
$lower strtolower($input);
    
    
$pattern[0] = "/\[b\](.*)\[\/b\]/";
    
$pattern[1] = "/\[url\=(.*)\](.*)\[\/url\]/";
    
    
$replace[0] = "<strong>$1</strong>";
    
$replace[1] = "<a href=\"$1\">$2</a>";
        
// your new subject is the string all lower case
    
$bbcoded preg_replace($pattern$replace$lower);
    echo 
$bbcoded;
}
codeBB("[b]hello![/b] [url=http://www.php.net/]PHP[/url]"); 
However, this will mean that you will have no capitals at all in your string. One moment while I work this out please. Of course you could just put in new rules for uppercase but I am fairly sure there is something to cut time.

Code:
[ b]bold[/b] not bold [ b]bold again[/b]
I would have to say that the best way to tackle this is create a seperate match for [ b] and [/b], for example:

PHP Code:
$pattern[0] = "/\[b\]/";
$pattern[1] = "/\[\/b\]/";
$replace[0] = "<strong>";
$replace[1] = "</strong>";

preg_replace($pattern$replace$input); 
mortisimus is offline  
Reply With Quote