3v4l.org

run code in 300+ PHP versions simultaneously
<?PHP $list = [ // an array list of string/regex that i want to check "lorem ipsum", // a words "example", // another word "/(nulla)/", // a regex "/[,.]/", // illustrate regex offset ]; $input_string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer quam ex, vestibulum sed laoreet auctor, iaculis eget velit. Donec mattis, nulla ac suscipit maximus, leo metus vestibulum eros, nec finibus nisl dui ut est. Nam tristique varius mauris, a faucibus augue."; // get matches and offset $match_count = 0; $matches = []; foreach($list as $v) { // make pattern from strings not enclosed in delimiters $pattern = $v[0] == '/' ? $v : "/".preg_quote($v, "/")."/i"; // using preg_match_all to get each match and offset if(preg_match_all($pattern, $input_string, $out, PREG_OFFSET_CAPTURE)) { foreach($out[0] AS $m) { echo $m[0]." matched at offset ".$m[1]."!\n"; } } }
Output for 8.1.23 - 8.1.33, 8.2.10 - 8.2.29, 8.3.0 - 8.3.27, 8.4.1 - 8.4.14
Lorem ipsum matched at offset 0! nulla matched at offset 139! , matched at offset 26! . matched at offset 55! , matched at offset 72! , matched at offset 103! . matched at offset 123! , matched at offset 137! , matched at offset 164! , matched at offset 192! . matched at offset 221! , matched at offset 250! . matched at offset 268!

preferences:
88.96 ms | 407 KiB | 5 Q