Php converts URLs in text into link tags using regular expressions,
Copy codeThe Code is as follows:
Function text2links ($ str = ''){
If ($ str = ''or! Preg_match ('/(http | www \. | @)/I', $ str) {return $ str ;}
$ Lines = explode ("\ n", $ str); $ new_text = '';
While (list ($ k, $ l) = each ($ lines )){
// Replace links:
$ L = preg_replace ("/([\ t] | ^) www \./I", "\ 1 http: // 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;
}