I'm writing a board... and I want to make it automatically parse URL's... now, I got it to work (sorta) with a preg_replace, but it'll double parse URL's that have [ url ] tags :. Here's the code I use right now:
function parse_urls($Text){
//First match things beginning with [url]http://[/url] (or other protocols)
$NotAnchor = '(?<!"|href=|href\s=\s|href=\s|href\s=)';
$Protocol = '(http|ftp|https):\/\/';
$Domain = '[\w]+(.[\w]+)';
$Subdir = '([\w\-\.,@?^=%&:\/~\+#]*[\w\-\@?^=%&\/~\+#])?';
$Expr = '/' . $NotAnchor . $Protocol . $Domain . $Subdir . '/i';
$Result = preg_replace( $Expr, "<a href=\"$0\" title=\"$0\" target=\"_blank\">$0</a>", $Text );
//Now match things beginning with [url]www.[/url]
$NotAnchor = '(?<!"|href=|href\s=\s|href=\s|href\s=)';
$NotHTTP = '(?<!:\/\/)';
$Domain = 'www(.[\w]+)';
$Subdir = '([\w\-\.,@?^=%&:\/~\+#]*[\w\-\@?^=%&\/~\+#])?';
$Expr = '/' . $NotAnchor . $NotHTTP . $Domain . $Subdir . '/i';
return preg_replace($Expr, "<a href=\"http://$0\" title=\"http://$0\" target=\"_blank\">$0</a>", $Result);
}
And here's the code for bb_code parsing:
//# Parse BB Code
function parse_bbcode($value, $qtemplate) {
$qtemplate = stripslashes($qtemplate);
$qtemplate = str_replace('$quote','\\1',$qtemplate);
$value = str_replace("[ b]", "<b>", $value);
$value = str_replace("[ /b]", "</b>", $value);
$value = str_replace("[ i]", "<i>", $value);
$value = str_replace("[ /i]", "</i>", $value);
$value = str_replace("[ u]", "<u>", $value);
$value = str_replace("[ /u]", "</u>", $value);
$value = preg_replace("/\[ color=(.+?)\](.+?)\[\/color]/","<font color=\"\\1\">\\2</font>",$value);
$value = preg_replace("/\[ size=(.+?)\](.+?)\[\/size]/","<font size=\"\\1\">\\2</font>",$value);
$value = preg_replace("/\[ url\](.+?)\[\/url]/","<a href=\"\\1\" target=\"_blank\">\\1</a>",$value);
$value = preg_replace("/\[ url=(.+?)\](.+?)\[\/url]/","<a href=\"\\1\" target=\"_blank\">\\2</a>",$value);
$value = preg_replace("/\[ img\](.+?)\[\/img]/","<img src=\"\\1\">",$value);
$value = preg_replace("/\[ quote\](.+?)\[\/quote]/",$qtemplate,$value);
return $value;
}
Would someone tell me how to get it to stop double parsing? Thanks 🙂.