Page 1 of 1

Genius Needed

Posted: Wed Feb 06, 2008 11:57 am
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

Re: Genius Needed

Posted: Thu Feb 07, 2008 9:51 pm
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 :)

Re: Genius Needed

Posted: Sat Feb 09, 2008 3:50 pm
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.