找到字符串中的最后一个超链接

时间:2014-07-15 21:37:53

标签: php regex

我想在字符串中找到最后一个超链接。超链接可以以下列之一开头:

的http://

的https://

市场://

PHP中是否有正则表达式方法可以找到它?

谢谢。

2 个答案:

答案 0 :(得分:3)

你可以使用这种基于正则表达式的负前瞻:

~\b(?:https?|market)://\S+?(?!.*?\b(?:https?|market)://)~i

答案 1 :(得分:0)

$regex = "/(http|https|market)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";

$text = "http://link.com some text in between https://link.com more text in between market://link.com";

if(preg_match_all($regex, $text, $url)) 
{
    echo $url[0][count($url[0])-1]; // Outputs: market://link.com
}