Is there any way to limit the number of matches that will be returned using preg_match_all
?
So for example, I want to match only the first 20 <p>
tags on a web page but there are 100 <p>
tags.
Cheers
$matches = array();
preg_match_all ( $pattern , $subject , $matches );
$twenty = array_slice($matches , 0, 20);
Just match all and slice the resulting array:
$allMatches = array ();
$numMatches = preg_match_all($pattern, $subject, $allMatches, PREG_SET_ORDER);
$limit = 20;
$limitedResults = $allMatches;
if($numMatches > $limit)
{
$limitedResults = array_slice($allMatches, 0, $limit);
}
// Use $limitedResults here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With