How to make preg_mach_all in different rows?

2
<div class="information di-ib mt4">
    Movie (1 eps)<br>
    Aug 2016 - Aug 2016<br>
    380,496 members
</div></div>

I want to do preg_match_all in this code, but I do not know how to specifically do the number of episodes.

    
asked by anonymous 03.06.2017 / 16:07

2 answers

2

If the text only contains data from a movie, you can use the preg_match :

if (preg_match("/\((?P<qtd>\d+) eps\)/", $data, $matches))
{
  echo "O filme possui ", $matches["qtd"], " episódio(s)", PHP_EOL;
}

The output for the text in question would be:

O filme possui 1 episódio(s)
  

See working at Ideone .

However, if the text has more than one movie information in fact, it will be necessary to use the preg_match_all :

if (preg_match_all("/\((?P<qtd>\d+) eps\)/", $data, $matches))
{
  print_r($matches["qtd"]);
}

The only difference is that $matches["qtd"] will be a list of values for all movies.

  

See working at Ideone .

The regular expression used in both cases is:

/\((?P<qtd>\d+) eps\)/

The goal is to find all groups in (X eps) format, with X integer.

  • The \( escapes the ( character and indicates the beginning of the desired group;
  • The (?P<qtd>\d+) part captures any sequence of numbers, generating the group named qtd ;
  • The eps part makes it necessary for the sequence of numbers to be followed by that text;
  • The \) part escapes the ) character and indicates the end of the desired group;
  • 03.06.2017 / 16:27
    0

    Using preg_match_all :

    <?php
    $dado = "
    <div class='information di-ib mt4'>
        Movie (1 eps)<br>
        Aug 2016 - Aug 2016<br>
        380,496 members
    </div>";
    preg_match_all("/(?<=\()[\d]/",$dado,$saida,PREG_PATTERN_ORDER);
    print_r($saida[0]);//Array ( [0] => 1 )
    ?>
    

    Ideone Test

    Explanation of regex

        
    03.06.2017 / 16:46