user704278
user704278

Reputation: 19

php regular expression to get the specific url

I would like to get the urls from a webpage that starts with "http://example.com/category/" from these tags below:

<td><a href="http://example.com/category/subcategory/product/257849" title="Sample Title">Test</a></td>

Note:

257849 = random number

Any suggestion would be very much appreciated.

Thanks!

Upvotes: 0

Views: 237

Answers (2)

James C
James C

Reputation: 14149

preg_match_all('#http://example.com/category[^"]+#', $text, $a);

The result will be in $a

Upvotes: 0

mario
mario

Reputation: 145482

Just specify the fixed base URL asis in the regex, and use [\w/]+ to match any combination of letters, numbers and the / slash afterwards:

preg_match('#http://example.com/category/[\w/]+#', $text, $match);
print $match[0];

And to extract all urls at once, use preg_match_all() instead.

Upvotes: 1

Related Questions