0
$sourcestring=$html;
preg_match_all('/<font color=\"#FFFFFF\"><b>(.*?)<\/b>/',$sourcestring,$match);
//echo "<pre>".print_r($match,true);

echo($match[0][0]);

I was wondering how I could loop through $match variable 32 times with a for loop or while loop? The only way I know how to print out the elements is manually type:

echo $match([0][1]); .. /// echo  $match([0][1]);

Can someone please explain to me how I can iterate through the array preg_match_all is making?

Andy Lester
  • 91,102
  • 13
  • 100
  • 152
user1370982
  • 9
  • 1
  • 7
  • 1
    With [`foreach`](http://php.net/foreach)`($match[0] as $entry)` – mario Apr 13 '13 at 00:38
  • Can you explain to me how this works? – user1370982 Apr 13 '13 at 00:40
  • Anyone help me with the last comment i posted? I read the other question you posted i didn't see when i search, but that only answers my first question. – user1370982 Apr 13 '13 at 01:13
  • **Don't use regular expressions to parse HTML**. You cannot reliably parse HTML with regular expressions, and you will face sorrow and frustration down the road. As soon as the HTML changes from your expectations, your code will be broken. See http://htmlparsing.com/php for examples of how to properly parse HTML with PHP modules that have already been written, tested and debugged. – Andy Lester Apr 13 '13 at 01:25

1 Answers1

2

Yes. See working example

<?php
$sourcestring="A string1 A string2 A string3 Of string4 to match";
preg_match_all('/string.{1}/',$sourcestring,$match);

$myMatches = "";

foreach($match[0] as $thisMatch){
   echo "$thisMatch\n";
   $myMatches .= "$thisMatch <br\>";
}

echo $myMatches;
bubba
  • 3,839
  • 21
  • 25
  • Ok say i have two for each loops iterating through one array and one through another array. foreach(match[0] as $matches){ echo "$matches" . "
    "; } foreach(match[0] as $nextMatch){ echo "$nextMatch" . "
    "; } How could i echo out the contents in each array to display like matches + nextMatch matches + nextMatch
    – user1370982 Apr 13 '13 at 00:50
  • I don't understand what you are asking at this point. I updated the answer to your original question to address what I imagine perhaps you are asking. – bubba Apr 13 '13 at 01:16