Online Help PHP regular expression notation.
All URLs that appear in the following text want to be in the form of 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 before the case of HTTP or HTTPS, want to replace http://www.baidu.com format, and then replaced by hyperlinks,
Kneel to the regular expression style.
Reply to discussion (solution)
On this basis changed the next
$s = ' "text www.baidu.com text 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. Thank
Class Convert
{
/*
* Urlを from でリンク form occupies
*/
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. '';
}
}