Regular expression when there are equal strings

Asked

Viewed 140 times

0

I need to take the value "R$ 0,00" in this string with a regular expression. I really don’t understand expressions, I only know the basics of the basic. There are several other values on the page with "R$" followed by the value, but I can not catch with my methods, always gives a random value of other "R$". I need something accurate. OBS: 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>
  • Catch with JS? show what you’ve tried

  • use the following function to catch strings: function GetStr($string, $start, $end){ &#xA;$str = explode($start, $string); &#xA;$str = explode($end, $str[1]); &#xA;return $str[0]; &#xA;} usage $estimate = Getstr($result, 'mber">R$','</div ');

  • her work and take the first and last character from an html to locate the text, most often works well, but with many identical strings and equal tags is not good.

  • Regular expressions to pick up information within an HTML? You’re calling Ctulhu and don’t notice, that’s why you feel fear in the dark. Regex to get things from HTML live in closets and come out when the lights are off to feed on the fear of children

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 regex, whereas the next it returns the result of each separate group of the regex sentence (as in mine only has 1 group it created only 1 index more).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.