tags:

views:

146

answers:

3

i have multiple strings containing a link like:

 <A HREF="http://www.testings2"&gt;testings2&lt;/A&gt;
 <A HREF="http://www.blabla"&gt;blabla&lt;/A&gt;
 <A HREF="http://www.gowick"&gt;gowick&lt;/A&gt;

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 HREF="" without using preg_replace function?

thanks!

+3  A: 
preg_match_all('/href="([^"]+)/i', $str, $matches);
var_dump($matches);
zerkms
your regex won't match the examples strings in my answer.
ghostdog74
actually it is. show complete code so we can see the proof of your words
zerkms
why don't you test your regex against my examples strings and see. also, while OP's case is simple, your regex is greedy and will fail if there are many `href` in one line
ghostdog74
it not fails in multiple href's in one line, and it's not greedy just because it's [^"].it worked with expected set of urls. ps: try check yours with HREF in lowercase, lol. and with lowercased </a>.
zerkms
as you wish: http://pastie.org/869974
ghostdog74
my code works with expected set of urls. and yours failed on </a>. dude, before criticize me - fix your "code". actually you get as input terrible set of cr*p, but in question it was proper html.
zerkms
sure, my code works on expected set of urls, plus malformed strings as well. if you don't like to be criticized, then don't come here.
ghostdog74
so - get yet another malformed string: <A HrEF="http://www.lol">testings2</a> <A HReF="http://www.lol2">blabla</a> parse it with your code. good luck.
zerkms
so i paste the sample malformed string to your post. fix it. after you will - we can continue here.
zerkms
and so i did. now go fix yours.
ghostdog74
you don't added "class" into any url.
zerkms
$str = preg_replace('![\r\n]!', '', $str);preg_match_all('/href="([^"]+)/i', $str, $matches);and this is mine, fixed ;-)
zerkms
+1  A: 
$str=<<<EOF
 <A href="http://
www.testings2">testings2</A> blah
 <A HrEF=
"http://www.blabla"&gt;blabla&lt;/A&gt; blah
 <A HREF="http://www.gowick"&gt;gowick&lt;/A&gt;
 <A
HREF="http://www.testing3"&gt;testing3&lt;/A&gt;
<a class="navigation" id="selected" href="http://somewhere.com"&gt;&lt;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
ghostdog74
lol. explode + strpos + preg_replace. @ghostdog74, your idea was to use as much string functions as you can apply to this task? ;-))
zerkms
Yeah, regex matching mechanisms are appropriate for this problem, not crazy string manipulations.
ladenedge
it not handle this: <A HrEF="lol" class="dude lol">testings2</a> <A HReF="lol2">blabla</a> :'-( fiiix pleeeeease
zerkms
it still fails due to class attribute
zerkms
A: 

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