While begging for regular expression writing, you can seek help from php regular expression writing.
All the URLs in the following text are to be changed to hyperlinks.
"Text www.baidu.com text
Www.html.com
Text http://www.baidu.com
Https://www.google.com"
The code is as follows:
Public function make_clickable ($ str ){
$ Str = preg_replace (''([^" = \ '>]) (http: // | https: // | ftp: // | ftps: // | www .) [^ \ s <] + [^ \ s <\.)]) 'I', '$1 $ 2', $ str );
Return $ str;
}
Problem:
When the text is www.baidu.com, the hyperlink becomes http: // localhost/www.baidu.com
Www without http or https, to replace with the http://www.baidu.com format, and then replace with hyperlink,
Find the regular expression.
Reply to discussion (solution)
On this basis, I changed
$ S = 'Your text www.baidu.comtext www.html. com text http://www.baidu.comhttps: // www.google.com "'; echo make_clickable ($ s); function make_clickable ($ str) {$ str = preg_replace (''([^" = \ '>]) (http: // | https: // | ftp: // | ftps: // | www .) [^ \ s <] + [^ \ s <\.)]) 'Ie', "_ link ('\ 1',' \ 2 ')", $ str); return $ str;} function _ link ($ text, $ link) {if (explode ('. ', $ link) [0] = 'WWW') {$ link = 'http ://'. $ link;} return $ text. ''. $ link. '';}
Very useful. Thanks
Class Convert
{
/*
* Is the URL encoded from? でリンクに
*/
Public function make_clickable ($ str)
{
$ Str = preg_replace (''([^" = \ ']) (http: // | https: // | ftp: // | ftps: // | www .) [^ \ s <] + [^ \ s <\.)]) 'Ie', "self: _ link ('\ 1', '\ 2')", $ str );
Return $ str;
}
Private static function _ link ($ text, $ link)
{
If (explode ('.', $ link) [0] = 'WWW '){
$ Link = 'http: // '. $ link;
}
Return $ text. ''. $ link .'';
}
}