ajsie
ajsie

Reputation: 79756

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?

Upvotes: 0

Views: 191

Answers (3)

ghostdog74
ghostdog74

Reputation: 342591

$str=<<<EOF
 <A href="http://
www.testings2">testings2</A> blah
 <A HrEF=
"http://www.blabla">blabla</A> blah
 <A HREF="http://www.gowick">gowick</A>
 <A
HREF="http://www.testing3">testing3</A>
<a class="navigation" id="selected" href="http://somewhere.com"><xsl:value-of
select="title" /></a>
EOF;

$s = preg_split("/<\/A>/i",$str);
$s = preg_replace("/\n+/","",$s);
$uris = preg_grep("/HREF/i",$s);
foreach($uris as $v){
  $fin = explode('">',$v);
  $t=preg_split('/href="/i',$fin[0] );
  print end($t)."\n";
}

output

# php test.php
http://www.testings2
http://www.blabla
http://www.gowick
http://www.testing3
http://somewhere.com

Upvotes: 0

zellio
zellio

Reputation: 32504

not sure how to apply this in PhP but it works in perl

/<a href="([^"]+)".+/i;

I assume it to be

preg_match( '/<a href="([^"]+)".+/i;', $str, $matches);

Upvotes: 0

zerkms
zerkms

Reputation: 254976

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

Upvotes: 4

Related Questions