开发者

help with regex pattern

开发者 https://www.devze.com 2022-12-23 11:29 出处:网络
i have multiple strings containing a link like: <A HREF=\"http://www.testings2\">testings2</A>

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.

i could do like:

 /".*?"/

but then the "" will come along. is there a way to just get the uri within the HRE开发者_如何学编程F="" without using preg_replace function?

thanks!


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


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);


$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
0

精彩评论

暂无评论...
验证码 换一张
取 消