- Output for 8.1.0 - 8.1.28, 8.2.0 - 8.2.19, 8.3.0 - 8.3.7
- 22 :: a: 1 versus 22 10 :: o: 2 versus 20 6 :: p: 1 versus 6 6 :: t: 2 versus 20 6
<?php
$text = "Ham followed now ecstatic use speaking exercise may repeated.
Himself he evident oh greatly my on inhabit general concern. It allowance
prevailed enjoyment in it. Calling observe for who pressed raising his. Can
connection instrument astonished unaffected his motionless preference.
Announcing say boy precaution unaffected difficulty alteration him.";
$word = "Potato";
$needleCharCounts = count_chars(strtolower($word), 1);
$haystackCharCounts = count_chars(strtolower($text), 1);
$result = null;
foreach ($needleCharCounts as $letter => $count) {
$result = min($result ?? PHP_INT_MAX, intdiv($haystackCharCounts[$letter] ?? 0, $count));
echo "$result :: " . chr($letter) . ": $count versus " . $haystackCharCounts[$letter] . "\n";
if ($result === 0) {
break;
}
}
echo $result ?? 0;
// "a" can be found 22 times.
// "o" can be found 20 times, so two "o"s can be found 10 times.
// "p" can be found 6 times.
// "t" can be found 20 times, so two "t"s can be found 10 times.