This article will share with you A piece of php function code that converts a url into A link using regular expressions. this code is very simple and practical. we recommend it to you here.
The 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 "," http://www.html "> \ 1 http: // www. ", $ l );
$ L = preg_replace ("/([\ t] | ^) ftp \./I", "\ 1ftp: // ftp.", $ l );
$ L = preg_replace ("/(http: \/[^) \ r \ n!] +)/I ",
"\ 1", $ l );
$ L = preg_replace ("/(https: \/[^) \ r \ n!] +)/I ",
"\ 1", $ l );
$ L = preg_replace ("/(ftp: \/[^) \ r \ n!] +)/I ",
"\ 1", $ l );
$ L = preg_replace (
"/([-A-z0-9 _] + (\. [_ a-z0-9-] +) * @ ([a-z0-9-] + (\. [a-z0-9-] +)/I ",
"\ 1", $ l );
$ New_text. = $ l. "\ n ";
}
Return $ new_text;
}