using preg_match_all for this

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
Dade72
Forum Newbie
Posts: 1
Joined: Fri May 09, 2008 1:50 pm

using preg_match_all for this

Post 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.
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: using preg_match_all for this

Post 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";
}
?>
User avatar
GeertDD
Forum Contributor
Posts: 274
Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium

Re: using preg_match_all for this

Post 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 ([^"]*+).
Post Reply