Genius Needed

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

Moderator: General Moderators

Post Reply
jk11uk
Forum Newbie
Posts: 1
Joined: Wed Feb 06, 2008 11:53 am

Genius Needed

Post by jk11uk »

Hi

i have a php script that takes a url and loads all the code from that page into a variable.

I then want to scan through that variable and pick out the first 5 URLs. So i would need something which looks for the first 5 times that: \<a href=''http:// ----some url-----''> happens and store the occurances into different variables.


can anyone work it out? i'm very stuck
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Genius Needed

Post by alex.barylski »

I'm no regex guru by any stretch of the word...but I htink thats what the curly braces are for, so instead of appending a + or ? after your URL match, you would use the {,5}

Just a guess :)
User avatar
GeertDD
Forum Contributor
Posts: 274
Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium

Re: Genius Needed

Post by GeertDD »

Two ways to tackle this situation pop up in my head. You could use preg_match_all() to match all URLs. Then just pick the first five out. Alternatively, you could put preg_match() in a loop. On every iteration you store the match and update the offset parameter. I'd go for the first method.
Post Reply