<?php
function findANeedle(string $haystack, array $needles) {
foreach ($needles as $needle) {
if (str_contains($haystack, $needle)) {
return $needle;
}
}
return null;
}
function hasANeedle(string $haystack, array $needles): bool {
foreach ($needles as $needle) {
if (str_contains($haystack, $needle)) {
return true;
}
}
return false;
}
$haystack = 'food fighters';
$needles = ['bar', 'foo'];
var_export(findANeedle($haystack, $needles));
echo "\n--\n";
var_export(hasANeedle($haystack, $needles));
- Output for 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.26, 8.4.1 - 8.4.13
- 'foo'
--
true
preferences:
77.5 ms | 406 KiB | 5 Q