- Output for 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.7
- 0
<?php
$haystack = "I like cats.";
$needles = ['cat' => 0, 'dog' => 1];
function regex(string $haystack, array $needles) {
$regex = implode('|', array_map('preg_quote', array_keys($needles)));
if (!preg_match("#$regex#", $haystack, $m)) {
throw new Exception('No needles were found in the haystack');
}
return $needles[$m[0]];
}
try {
var_export(regex($haystack, $needles));
} catch (Exception $e) {
echo $e->getMessage();
}