bbcode hyperlink issue (help!!)
- by Jorm
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