The writing of the regular expression of kneeling

Source: Internet
Author: User
Tags php regular expression
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. '';
}
}

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.