Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

count occurrences in preg_replace regex

Tags:

php

I know that with I can find of occurrences with preg_match_all
i.e.

<?php
$text = 'here is JUICE. it is good JUICE...';
$counted    = preg_match_all('/JUICE/',$text,$empt);
echo $counted;
?>

but how can I count occurrences with preg_replace?

like image 563
T.Todua Avatar asked Dec 30 '25 16:12

T.Todua


1 Answers

As per the manual, there is an optional 5th parameter $count, which will be set to the number of replacements performed:

preg_replace($pattern, $replacement, $subject, -1, $count)

The 4th parameter is the $limit on the number of replacements. -1 means no limit.

like image 149
Tom Fenech Avatar answered Jan 01 '26 07:01

Tom Fenech



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!