Hello Everyone,
First of all thank for taking the time to look at my post.
Ok, I have the following code in my coded forums which auto find and link links. It works great but i am also using my BBCode function which is used to echo out the post to replace the BB Codes with the HTML. So what's happening is when the user adds a link without the [link=][link] bbcodes it does replace it with the bbcodes but it also does it for images which messes up the img bb code as its doing this:
<img src="<a href="http://www.google.com">http://www.google.com</a>"" width="400" height="360">
Heres the regex for the auto link:
$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
preg_match_all($reg_exUrl, $string, $matches);
$usedPatterns = array();
foreach($matches[0] as $pattern){
if(!array_key_exists($pattern, $usedPatterns)){
$usedPatterns[$pattern]=true;
$string = str_replace($pattern, '[link='.$pattern.']'.$pattern.'[/link]', $string);
}
}
And here is the code i'm using and encoutering problem with:
<?
function BBCode ($string) {
$search = array(
'#\[b\](.*?)\[/b\]#',
'#\[i\](.*?)\[/i\]#',
'#\[u\](.*?)\[/u\]#',
'#\[youtube\](.*?)\[/youtube\]#',
'#\[img\](.*?)\[/img\]#',
'#\[link=(.*?)\](.*?)\[/link\]#',
'#\[code\](.*?)\[/code\]#'
);
$replace = array(
'<b>\\1</b>',
'<i>\\1</i>',
'<u>\\1</u>',
'<br /><br /><center><object width="500" height="306"><param name="movie" value="http://www.youtube.com/v/\\1?fs=1&hl=en_US&color1=0x234900&color2=0x4e9e00"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/\\1?fs=1&hl=en_US&color1=0x234900&color2=0x4e9e00" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="500" height="306"></embed></object></center><br /><br />',
'<br /><br /><a href="\\1" target="_blank" ><h3>Enlarge Image</h3></a><a href="\\1" target="_blank" ><img src="\\1" style="border:none; background-color:transparent;" width="500" height="306"></a><br /><br />',
'<a href="\\1" target="_blank" >\\2</a>',
'<code>\\1</code>'
);
if($_COOKIE['userloggedin'] == ""){
$replace['5'] ="<br /><br /><font color='red'>[ LINKS FOR REGISTERED USERS ONLY, <a href='login' class='cp'><strong><u>LOGIN</u></strong></a> OR <a href='register' class='cp'><strong><u>REGISTER</u></strong></a> TO VIEW ]</font><br /><br />";
}else{
$replace['5'] = '<a href="\\1" target="_blank">\\2</a>';
}
$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
preg_match_all($reg_exUrl, $string, $matches);
$usedPatterns = array();
foreach($matches[0] as $pattern){
if(!array_key_exists($pattern, $usedPatterns)){
$usedPatterns[$pattern]=true;
$string = str_replace($pattern, '[link='.$pattern.']'.$pattern.'[/link]', $string);
}
}
return preg_replace($search , $replace, $string);
$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
}
?>
Thanks,
Judgey