Page 1 of 1

using preg_match_all for this

Posted: Fri May 09, 2008 2:00 pm
by Dade72
Hi, I have a problem I can't seem to get past so its time to ask for help, I'm trying to extract url's
from a link that looks like this:

Code: Select all

<a href="http://www.somedomain.com/linktopage">[1]</a>
my guess was to use something like this, but I'm probably way off, but I used it because
it was very important to me it match all links with [1] within the <A>:

Code: Select all

#href://(.*?)">\[1\]</a>#
any help would be appreciated.

Re: using preg_match_all for this

Posted: Sat May 10, 2008 3:11 pm
by prometheuzz
Try this:

Code: Select all

#!/usr/bin/php
<?php
$text = '<a href="http://www.somedomain.com/linktopage">[1]</a>';
if(preg_match('#href="(.*?)">\[1\]</a>#', $text, $matches)) {
  $url = $matches[1];
  print "$url\n";
}
?>

Re: using preg_match_all for this

Posted: Sun May 11, 2008 7:03 am
by GeertDD
Prometheuzz's regex looks fine. Just put it in preg_match_all() now.

Note that you may gain some speed by replacing the (.*?) part with ([^"]*+).