preg_match help

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

Moderator: General Moderators

Post Reply
*nixMe
Forum Newbie
Posts: 5
Joined: Sun Mar 09, 2008 3:57 pm

preg_match help

Post by *nixMe »

I'm pretty new to regex coding so I'm sure this issue is just something I'm missing in my regex and I need some help. I'm writing a web service class that queries the freedb.org search page for matches on discs. There's a CURL event that calls the search page and then the body of the request is parsed for all the links to the text files of all the discs. The regex to do that is:

Code: Select all

 
//$parseForLinks contains the body of the search request.
if($m = preg_match('@(?:[Hh][Tt][Tt][Pp]://)?(?:[Ww]{3}\.)?(?:freedb\.org/freedb/[A-Za-z0-9]+/[A-Za-z0-9]+/?)@',$parseForLinks,$matches)) {
            print_r($matches);
        }
 
The above code works fine but the problem I'm having is it only matches 1 link when I want all the links within the page. My initial solution to this problem was to split the body into an array (each index containing one line of the body) and then loop through each line for matches but I still only get 1 link. Is there a way to match an arbitrary amount or is my regex not correct? Please help.

Here's an example link for you to see what I'm trying to parse out.

http://www.freedb.org/freedb_search.php ... llcats=YES
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: preg_match help

Post by John Cartwright »

Have you looked at preg_match_all()?
*nixMe
Forum Newbie
Posts: 5
Joined: Sun Mar 09, 2008 3:57 pm

Re: preg_match help

Post by *nixMe »

HAH!! Yeah that works. Thanks for the heads up.
Post Reply