KenBoyer
KenBoyer

Reputation: 41

Using PHP's preg_match_all to extract a URL

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!

Upvotes: 1

Views: 3611

Answers (2)

reikyoushin
reikyoushin

Reputation: 2021

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

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

you should be able to get ALL the urls on the content available in $matches[1]..

Note: this will only get urls with the {rssfeed:url()} format, not all the urls in the content.

you can try this here: http://www.spaweditor.com/scripts/regex/index.php

Upvotes: 2

Jean
Jean

Reputation: 7673

Could this work for you?

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

I use it to match URLs in text.

Example:

$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]);

Output:

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

Note:

There is also a nice article on Daring Fireball called An Improved Liberal, Accurate Regex Pattern for Matching URLs that could be interesting for you.

Upvotes: 4

Related Questions