Linkify Regex Function PHP Daring Fireball Method

前端 未结 2 1733
迷失自我
迷失自我 2020-12-06 14:57

So, I know there are a ton of related questions on SO, but none of them are quite what I\'m looking for. I\'m trying to implement a PHP function that will convert text URLs

相关标签:
2条回答
  • 2020-12-06 15:10

    I was looking to just get the urls from a string using the same regex from the answer above by d_inevitable and wasn't looking to turn them into links or care about the rest of the string, I only wanted the urls with in the string so this is what I did. Hope it helps.

    /**
     * Returns the urls in an array from a string.
     * This dos NOT return the string, only the urls with-in.
     */
    function get_urls($str){
    
        $regex = '(?xi)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?«»“”‘’]))';
        preg_match_all("#$regex#i", $str, $matches);
        $urls = $matches[0];
        return $urls;
    
    }
    
    0 讨论(0)
  • 2020-12-06 15:19

    Try this:

    $pattern = '(?xi)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`\!()\[\]{};:\'".,<>?«»“”‘’]))';     
    return preg_replace("!$pattern!i", "<a href=\"\\0\" rel=\"nofollow\" target=\"_blank\">\\0</a>", $str); 
    

    PHP's preg function do need delimiters. The i at the end makes it case-insensitive

    Update

    If you use # as the delimiter, you wan't need to escape the ! in the pattern as such use the original pattern string (the pattern does not have a #): "#$pattern#i"

    Update 2

    To ensure that the links are correct, do this:

    $pattern = '(?xi)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?«»“”‘’]))';
    return preg_replace_callback("#$pattern#i", function($matches) {
        $input = $matches[0];
        $url = preg_match('!^https?://!i', $input) ? $input : "http://$input";
        return '<a href="' . $url . '" rel="nofollow" target="_blank">' . "$input</a>";
    }, $str); 
    

    This will now append http:// to the urls so that browser doesn't think it is a relative link.

    0 讨论(0)
提交回复
热议问题