0

How can i get the numbers 148.42 from the markup below with preg_match_all? They all have the same class, so i have no clue how to get the numbers.

I have 2 variables - $usd_kzt and $eur_kzt and i need to store the digit values from the code below in these two variables.

<td width="15"><input id="idval7" name="idval" class="idValI" value="5" type="checkbox"></td> <td class="gen7" align="left">&nbsp;1 ДОЛЛАР США</td>
<td class="gen7" align="center">USD / KZT</td>
<td class="gen7" align="center">148.42</td>

<td class="gen7" align="center">EUR / KZT</td>
<td class="gen7" align="center">200.42</td>
<td class="gen7" valign="middle" width="10" align="left">
<img src="images/whup.gif"></td>
<td class="gen7" align="center"></td>

Tried something like this:

preg_match_all('/<td\s+.*?>(.*)<\/td>/is', $data, $matches);
print_r($matches); // returns nothing

thanks in advance

Alexander Kim
  • 17,304
  • 23
  • 100
  • 157

2 Answers2

1

Try this:

preg_match_all('/<td[^>]*>(.*)<\/td>/', $data, $matches);

Here's what you'll get from print_r($matches):

Array
(
    [0] => Array
        (
            [0] => <td width="15"><input id="idval7" name="idval" class="idValI" value="5" type="checkbox"></td> <td class="gen7" align="left">&nbsp;1 ДОЛЛАР США</td>
            [1] => <td class="gen7" align="center">USD / KZT</td>
            [2] => <td class="gen7" align="center">148.42</td>
            [3] => <td class="gen7" align="center">EUR / KZT</td>
            [4] => <td class="gen7" align="center">200.42</td>
            [5] => <td class="gen7" align="center"></td>
        )

    [1] => Array
        (
            [0] => <input id="idval7" name="idval" class="idValI" value="5" type="checkbox"></td> <td class="gen7" align="left">&nbsp;1 ДОЛЛАР США
            [1] => USD / KZT
            [2] => 148.42
            [3] => EUR / KZT
            [4] => 200.42
            [5] => 
        )

)

As you can see, you can easily get to your data:

echo $matches[1][2]; // logs "148.42"
Joseph Silber
  • 214,931
  • 59
  • 362
  • 292
1

If you just want the numbers you can try something like:

preg_match_all('/(?:>)(\d+\.?\d*)(?:<)/', $str, $matches);

It only looks for the numbers in the tables

$matches brings back

Array
(
    [0] => Array
        (
            [0] => >148.42<
            [1] => >200.42<
        )
    [1] => Array
        (
            [0] => 148.42
            [1] => 200.42
        )
)
Biotox
  • 1,563
  • 10
  • 15