Alex Rivera | Logout

php regex to get string inside href tag

Asked 2010-10-22T22:09:08.113
17

I need a regex that will give me the string inside an href tag and inside the quotes also.

For example i need to extract theurltoget.com in the following:

<a href="theurltoget.com">URL</a>

Additionally, I only want the base url part. I.e. from http://www.mydomain.com/page.html i only want http://www.mydomain.com/

Edit
Report

1 Answer

0

This will handle the case where there are no quotes around the URL.

/<a [^>]*href="?([^">]+)"?>/

But seriously, do not parse HTML with regex. Use DOM or a proper parsing library.

answered 2010-10-22T22:14:35.970

Your Answer