0

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!

1

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

It's unlikely but there are other characters like % that could be valid and appear in the URL
0
preg_match_all('#http://example.com/category[^"]+#', $text, $a);

The result will be in $a

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.