Using foreach with Regex

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

Moderator: General Moderators

Post Reply
anthony88guy
Forum Contributor
Posts: 246
Joined: Thu Jan 20, 2005 8:22 pm

Using foreach with Regex

Post by anthony88guy »

My goal: Search numerous pages against 2 variables if they are true it will echo them out and continue searching.

I have my regex working (for the most part).


1. When it finds a match, how do I give each piece of information a variable? Like right now its just " id=19404">cradledbydeath 502 13,953 Draconians"
ex.
$var1 = "id=19404">";
$var2 = "cradledbydeath";

2. Why is it matching and returning "id=19404", when I’m trying to get the id number?

3. How would I be able to use a foreach(), so once it finds a match it doesn’t stop searching?

Thank you very much; I’ve been trying numerous things for an hour now with no go.
Last edited by anthony88guy on Thu Sep 15, 2005 9:49 pm, edited 1 time in total.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

First and foremost, read the docs. They can answer your questions: http://us2.php.net/manual/en/function.preg-match.php

1. Use a $matches array to echo out subpatterns (which you haven't set any)
2. You're not capturing any subpatterns with (), so it's returning the whole kaboodle that matched it
3. Try preg_match_all - http://us2.php.net/manual/en/function.p ... ch-all.php
anthony88guy
Forum Contributor
Posts: 246
Joined: Thu Jan 20, 2005 8:22 pm

Post by anthony88guy »

EDIT

Fix, a problem I was having now I have another.

Now when it outputs it only outputs 6 lines worth. And I have 6 sub patterns, but i dont know why it is limiting itself to 6 lines worth.

Thank you again.
Last edited by anthony88guy on Thu Sep 15, 2005 9:48 pm, edited 1 time in total.
User avatar
shoebappa
Forum Contributor
Posts: 158
Joined: Mon Jul 11, 2005 9:14 pm
Location: Norfolk, VA

Post by shoebappa »

I realized the real problem after I wrote the below : ) Good tips I think, but your counting the wrong part of the $match multideminsional array. Try count($match[0])


Read the tips I typed before I realized your real problem if you like...

Whenever I run into problems with regex it usually eats too much or not enough, or it's too sensitive. I usually use a combination of case insensative (i), ignore whitespace (s), and greedy flags (U).

Like #regex#siU

I usually use the si combo, and add the U when something doesn't work quite right.

I also usually have problems with .* eating too much, and I think the greedy flag helps. But usually I try to narrow it down to something it's not, which is usually HTML. So I'd say [^<]* or [^>]* in place of .*

Your [\n\s]* might work, but I always thought \s represented any whitespace (including \n) so that means there'd need to be a space and a new line at least once (I could be wrong)
anthony88guy
Forum Contributor
Posts: 246
Joined: Thu Jan 20, 2005 8:22 pm

Post by anthony88guy »

Thanks.

Concerning my outputting problem.


At the bottom you will see "print_r($match):", thats when i

Code: Select all

<?php print_r($match); ?>
my output is weird. Its all mixed, if someone could show me the light that would be great, you've been much help already. That’s why I love this forum.
Last edited by anthony88guy on Thu Sep 15, 2005 9:48 pm, edited 1 time in total.
User avatar
shoebappa
Forum Contributor
Posts: 158
Joined: Mon Jul 11, 2005 9:14 pm
Location: Norfolk, VA

Post by shoebappa »

It looks right to me. Try viewing the source to see the pretty output of print_r. Also the $match[0][#] will be the whole match, with $match[1-6][#] being your subpatterns.

Maybe I'm not seeing the problem.
anthony88guy
Forum Contributor
Posts: 246
Joined: Thu Jan 20, 2005 8:22 pm

Post by anthony88guy »

shoebappa wrote:It looks right to me. Try viewing the source to see the pretty output of print_r. Also the $match[0][#] will be the whole match, with $match[1-6][#] being your subpatterns.

Maybe I'm not seeing the problem.
Maybe its not a problem, but its not working the way I intend it to work. I figured out that $match[0] would print out the full match. But my for() loop only prints out 6 matches.

What im trying to do is being able to make an if statement on one of the sub-patterns. For instance,

Code: Select all

if($match[3] >= 5){
    echo "Match 3 is greater than";
}
User avatar
shoebappa
Forum Contributor
Posts: 158
Joined: Mon Jul 11, 2005 9:14 pm
Location: Norfolk, VA

Post by shoebappa »

Code: Select all

$site = file_get_contents($url); 

echo $site; 
/* matching: 
href="stats.php?id=[DYNAMIC DATA]">[DYNAMIC DATA]</A></TD> 
<TD style="PADDING-RIGHT: 20px" align=right>[DYNAMIC DATA]</TD> 
<TD align=right>[DYNAMIC DATA]</TD> 
<TD align=left>[DYNAMIC DATA]</TD> 
<TD style="PADDING-RIGHT: 20px" align=right>[DYNAMIC DATA] Gold 
*/ 

$found = preg_match_all('#id=([0-9]*)">(.*)</A></TD>[\n\s]*<TD style="PADDING-RIGHT: 20px" align=right>(.*)</TD>[\n\s]*<TD align=right>(.*)</TD>[\n\s]*<TD align=left>(.*)</TD>[\n\s]*<TD style="PADDING-RIGHT: 20px" align=right>(.*) Gold#', $site, $match); 

// note the count($match[0]) and the way I reference the third match.

for ($i = 0; $i < count($match[0]); $i++) { 
   echo "ID: " . $match[1][$i] . " "; 
   echo "NAME: " . $match[2][$i] . " "; 
   echo "RANK: " . $match[3][$i] . " "; 
   if($match[3][$i] >= 5){ 
      echo "Match 3 is greater than"; 
   } 
   echo "MEN: " . $match[4][$i] . " "; 
   echo "RACE: " . $match[5][$i] . " "; 
   echo "GOLD: " . $match[6][$i] . "<br>"; 
} 
?>
anthony88guy
Forum Contributor
Posts: 246
Joined: Thu Jan 20, 2005 8:22 pm

Post by anthony88guy »

Nevermind, I fixed it myself. Thanks A Bunch guys.
Post Reply