bbcode hyperlink issue (help!!)
        Posted  
        
            by Jorm
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Jorm
        
        
        
        Published on 2010-04-11T08:45:35Z
        Indexed on 
            2010/04/11
            10:03 UTC
        
        
        Read the original article
        Hit count: 506
        
I'm having an annoying :)
I use regexes from this: http://forums.codecharge.com/posts.php?post_id=77123
if you enter [url]www.bob.com[/url] it leads too http://localhost/test/www.bobsbar.com
So I added before http://$1 in the replacement. That fix it but then [url]http://www.bob.com[/url] will lead to http://http://www.bobsbar.com
How would you fix this? I want my users to be able to post links with AND without http:// and i want it to redirect to the site -_-
Hope you understand this.
Jorm
Edit
function bbcode_format($str)
{
$str = htmlentities($str);
$find = array(  
    '/\[url\](.*?)\[\/url\]/is', // hyperlink
    '/\[url\](http[s]?:\/\/)(.*?)\[\/url\]/is' // hyperlink http-protocol
);
$replace = array(
    '<a href="$1" rel="nofollow" title="$1">$1</a>',
    '<a href="$1$2" rel="nofollow" title="$2">$2 THIS WORKS</a>'
);
$str = preg_replace($find, $replace, $str);  
return $str;
}
both www.bob.com and http://www.bob.com uses the first replacement
© Stack Overflow or respective owner