So I have this snippet:

preg_match("/<td align=\"right\" class=\"text\">(.*?)<\/td>/s", $read, $print1);

With the '?' in there, it stops at the first match it finds. Now here is where I am lost. There are approximately 12 other strings exactly like the above, but with different number outputs.

I need to snag #1 (can do that), #3, and #7.

Is there any easy way to tell the function which selection of <td align=\"right\" class=\"text\">(.*?)<\/td> to look at?

Thanks ahead of time!

    preg_match_all() may be the function you're looking for.

    http://ca.php.net/preg_match_all

    It matches all of the occurrences of a pattern and returns them as an array. Very handy.

    So for your case, you could return all of the <td>'s, then loop through them to find the one(s) you want.

    Hope this helps.

    Cheers,

    Lux

      Thanks for the prod in the right direction. I had not yet tried preg_match_all as I am not very proficient with Arrays (yet).

      Here is what I tried so far, this is just outputting the word "Array". So I am partially there, heh.

      preg_match_all("/<td align=\"right\" class=\"text\">(.*)<\/td>/s", $read, $out);
      
      echo $out[0];
      echo $out[2];
      echo $out[6];
      

        OK, here is where I get lost. I do the following: print_r($out);

        This gives me the following:

        Array ( [0] => Array ( [0] => 1212.70 [1] => 1161.62 [2] => 0 [3] => 0 [4] => 0 [5] => -84 [6] => 10 [7] => 105 [8] => 8 [9] => 0 [10] => 0 [11] => 0 [12] => 03:13:15 [13] => 2004-11-11 01:01:52 [14] => 240 [15] => 209 [16] => 1.15 [17] => 1.24 [18] => 6 [19] => 3 [20] => 8 [21] => 3.33% [22] => 7 [23] => 3.35% [24] => 0 [25] => 0 [26] => 0 [27] => 0.00% [28] => 0.00 [29] => 0 [30] => 0.00% [31] => 0 [32] => 0 [33] => 23 [34] => 178 [35] => 0 / 2 [36] => 0.00% [37] => 0 / 0 [38] => 0.00% [39] => 0 / 105 [40] => 0.00% [41] => ) [1] => Array ( [0] => 1212.70 [1] => 1161.62 [2] => 0 [3] => 0 [4] => 0 [5] => -84 [6] => 10 [7] => 105 [8] => 8 [9] => 0 [10] => 0 [11] => 0 [12] => 03:13:15 [13] => 2004-11-11 01:01:52 [14] => 240 [15] => 209 [16] => 1.15 [17] => 1.24 [18] => 6 [19] => 3 [20] => 8 [21] => 3.33% [22] => 7 [23] => 3.35% [24] => 0 [25] => 0 [26] => 0 [27] => 0.00% [28] => 0.00 [29] => 0 [30] => 0.00% [31] => 0 [32] => 0 [33] => 23 [34] => 178 [35] => 0 / 2 [36] => 0.00% [37] => 0 / 0 [38] => 0.00% [39] => 0 / 105 [40] => 0.00% [41] => ) )

        So that at least tells me I am pulling numbers, and far more than the 13 or 14 I thought were in my dataset. My question is how do I echo particular ones?

        echo $out[14]; does not produce anything, but should echo out "240".

        Any ideas?

          What you get from preg_match_all() is a 2-dimensional array. So you can access it like this, for example:

          <?php
          
          $html = '...some html...';
          
          preg_match_all ('|<td align="right" class="text">(.*?)</td>|s', $html, $matches, PREG_SET_ORDER);
          
          foreach ($matches as $match) {
             echo 'Matched: ' . $match[1] . '<br />';
          }
          
          // or to request a specific match:
          
          echo $matches[5][1];
          
          ?>
          

          In the specific match reference above, the [5] means "the fifth match", while the [1] means "the first () of this match". [0] in place of [1] would mean "the full text of the match" ie. "<td...>...</td>".

          Hope this helps.

          Cheers,

          Lux

            Write a Reply...