phpregexhtml-parsinghreftext-extraction

Get href values from all hyperlinks in an HTML string


I have multiple strings containing a link like:

 <A HREF="http://www.testings2">testings2</A>
 <A HREF="http://www.blabla">blabla</A>
 <A HREF="http://www.gowick">gowick</A>

I want to use a regex pattern that gets the URI within the href attribute declaration.

I could do like:

 /".*?"/

but then the "" will come along. Is there a way to just get the URI within the HREF="" without using preg_replace() function?


Solution

  • preg_match_all('/href="([^"]+)/i', $str, $matches);
    var_dump($matches);