3v4l.org

run code in 500+ PHP versions simultaneously
<?php function get_found_strings(string $haystack, array $needles): array { $regex = '/' . str_replace('\|', '|', preg_quote(implode('|', $needles))) . '/'; preg_match_all($regex, $haystack, $matches); return $matches[0]; } $string1 = "Example! Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed tortor ipsum, faucibus iaculis dapibus ut, mollis vel diam."; $array_to_find = [ "#Example", "Example!", "Example?" ]; var_dump(get_found_strings($string1, $array_to_find));
Output for 8.1.4 - 8.1.34, 8.2.10 - 8.2.30, 8.3.0 - 8.3.30, 8.4.1 - 8.4.18, 8.5.0 - 8.5.7
array(1) { [0]=> string(8) "Example!" }

preferences:
59.67 ms | 862 KiB | 4 Q