使用Regex查找字符串中的所有链接

时间:2014-07-17 08:30:21

标签: php regex

我希望使用Regex查找字符串中链接的所有实例。我需要能够只返回链接的计数,而不是它们自己的值。

Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!
<a href="www.google.co.uk">www.google.co.uk</a>
<a href="www.google.co.uk">www.google.co.uk</a>
<a href="www.google.co.uk">www.google.co.uk</a>

由于

3 个答案:

答案 0 :(得分:2)

$re = '/<a href=\\"([^\\"]*)\\">(.*)<\\/a>/iU'; 
$str = "Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!\n<a href=\"www.google.co.uk\">www.google.co.uk</a>\n<a href=\"www.google.co.uk\">www.google.co.uk</a>\n<a href=\"www.google.co.uk\">www.google.co.uk</a>"; 

preg_match_all($re, $str, $matches);

echo count($matches);

上面应该捕获所有链接 哪个会返回3

答案 1 :(得分:1)

您可以在JS中使用前端方法来计算链接或修改链接。此方法用于替换字符串中的链接,该链接位于Detect URLs in Text下的stackoverflow。

    function urlify(text) {
        var urlRegex = /(https?:\/\/[^\s]+)/g;
        return text.replace(urlRegex, function(url) {
            return '<a href="' + url + '">' + url + '</a>';
        })
        // or alternatively
        // return text.replace(urlRegex, '<a href="$1">$1</a>')
    }

    var text = "Find me at http://www.example.com and also at http://stackoverflow.com";
    var html = urlify(text);

    // html now looks like:
    // "Find me at <a href="http://www.example.com">http://www.example.com</a> and also at <a href="http://stackoverflow.com">http://stackoverflow.com</a>"

以下是替换链接的PHP变体

// The Regular Expression filter
$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";

$matchCounter = 0;

// The Text you want to filter for urls
$text = "The text you want to filter goes here. http://google.com";

// Check if there is a url in the text
if(preg_match($reg_exUrl, $text, $url)) {
       // make the urls hyper links
       echo preg_replace($reg_exUrl, "<a href="{$url[0]}">{$url[0]}</a> ", $text);
       $matchCounter++;
} else {

       // if no urls in the text just return the text
       echo $text;
}

// Return $matchCounter for the matches in the string

亲切的问候 Jan Biasi

答案 2 :(得分:1)

我可能会使用与此类似的东西而不是正则表达式:

$dom = new DOMDocument();
$dom->loadHTML($input);
$xpath = new DOMXPath($dom);
$links = $xpath->query('//a[href]');

printf("found %d links\n", $links->length);

foreach($links as $link)
{
    printf("link to: %s\n", $link->getAttribute('href'));
}

请注意,我在SO文本框中键入了它,它可能包含错误。