Regular expression when there are equal strings

0

I need to get the value "$ 0.00" in this string with a regular expression. I really do not understand expressions, I just know the basics of the basics. There are several other values on the page with "$" followed by the value, but I can not get my methods, it always gives a random value of other "R $". I need something accurate. NOTE: In these divs this is the only value.

<div class="stat-number col-md-12 margin-top-10">
     <div class="title">Programado (Saldo)</div>
     <div class="number">
          R$ 0,00
      </div>
</div>
    
asked by anonymous 16.11.2017 / 04:06

1 answer

1

I think what you're looking for is:

<?php 
// Simulando uma string contendo valores aleatórios
$string = "Ola tenho R$ 35.00 e tambem R$ 0.00 com R$ 127335.98 reais";

// Variável onde armazenarei os resultados
$resultado = Array();

// Regra expressão regular
preg_match_all('/(R\$\ [0-9]*.[0-9]{0,2})/', $string, $resultado);

echo "<pre>";
print_r($resultado);
echo "</pre>";

?>

The output of this script will be:

Array
(
    [0] => Array
        (
            [0] => R$ 35.00
            [1] => R$ 0.00
            [2] => R$ 127335.98
        )

    [1] => Array
        (
            [0] => R$ 35.00
            [1] => R$ 0.00
            [2] => R$ 127335.98
        )

)

It returns 2 times the same result because the first index "0" returns the FULL MATCH which is the rule stipulated in the regex, the next one it returns the result of each group separated from the regex sentence (as in mine only has 1 group he created only 1 more index).

    
16.11.2017 / 11:31