<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.
<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.
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.
\(
escapes the (
character and indicates the beginning of the desired group; (?P<qtd>\d+)
part captures any sequence of numbers, generating the group named qtd
; eps
part makes it necessary for the sequence of numbers to be followed by that text; \)
part escapes the )
character and indicates the end of the desired group; 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 )
?>