1

I have been struggling for a while now to make the following work. Basically, I'd like to be able to extract a URL from an expression contained in an HTML template, as follows:

{rssfeed:url(http://www.example.com/feeds/posts/default)}

The idea is that, when this is found, the URL is extracted, and an RSS feed parser is used to get the RSS and insert it here. It all works, for example, if I hardcode the URL in my PHP code, but I just need to get this regex figured out so the template is actually flexible enough to be useful in many situations.

I've tried at least ten different regex expressions, mostly found here on SO, but none are working. The regex doesn't even need to validate the URL; I just want to find it and extract it, and the delimiters for the URL don't need to be parens, either.

Thank you!

4

2 回答 2

4

这对你有用吗?

'@((https?://)?([-\w]+\.[-\w\.]+)+\w(:\d+)?(/([-\w/_\.]*(\?\S+)?)?)*)@'

我用它来匹配文本中的 URL。

例子:

$subject = "{rssfeed:url(http://www.example.com/feeds/posts/default)}";
$pattern ='@((https?://)?([-\w]+\.[-\w\.]+)+\w(:\d+)?(/([-\w/_\.]*(\?\S+)?)?)*)@';    
preg_match_all($pattern, $subject, $matches);

print($matches[1][0]);

输出:

http://www.example.com/feeds/posts/default

笔记:

还有一篇关于 Daring Fireball 的好文章,名为“An Improvement Liberal, Accurate Regex Pattern for Matching URLs ”,您可能会感兴趣。

于 2013-04-19T21:24:14.117 回答
2

/\{rssfeed\:url\(([^)]*)\)\}/

preg_match_all('/\{rssfeed\:url\(([^)]*)\)\}/', '{rssfeed:url(http://www.example.com/feeds/posts/default)}', $matches, PREG_PATTERN_ORDER);
print_r($matches[1]);

您应该能够获得$matches[1]..中可用内容的所有 url

注意:这只会获取{rssfeed:url()}格式的 url,而不是内容中的所有 url。

你可以在这里试试这个:http ://www.spaweditor.com/scripts/regex/index.php

于 2013-04-19T21:30:29.993 回答