This article is an example of how PHP automatically adds links to URLs. Share to everyone for your reference. The implementation methods are as follows:
Here automatically match the page URL, including Http,ftp, and so on, automatically add links to the URL
function text2links ($str = ') {if ($str = = ' or!preg_match (') (http|www\.|
@)/I ', $str)) {return $str;} $lines = explode ("\ n", $str);
$new _text = '; while (the list ($k, $l) = each ($lines)) {//replace links: $l = Preg_replace ("/[\t]|^) www\./i", "\\1http://www.", $l
);
$l = Preg_replace ("/([\t]|^) ftp\./i", "\\1ftp://ftp.", $l); $l = Preg_replace ("/(http:\/\/[^) \r\n!]
+/I "," <a href=\ "\\1\" >\\1</a> ", $l); $l = Preg_replace ("/(https:\/\/[^) \r\n!]
+/I "," <a href=\ "\\1\" >\\1</a> ", $l); $l = Preg_replace ("/(ftp:\/\/[^) \r\n!]
+/I "," <a href=\ "\\1\" >\\1</a> ", $l); $l = Preg_replace ("/" [-a-z0-9_]+ (\.[ _a-z0-9-]+) *@ ([a-z0-9-]+ \.[
a-z0-9-]+) +)/I "," <a href=\ "mailto:\\1\" >\\1</a> ", $l); $new _text. = $l. "
\ n ";
return $new _text;
}//Use example: $text = "Welcome www.jb51.net:-)";
Print text2links ($text);
I hope this article will help you with your PHP programming.