<?PHP $list = [ // an array list of string/regex that i want to check "lorem ipsum", // a words "example", // another word "/(nulla)/", // a valid regex "/[,.]/", // a valid regex "^dolor^", // a valid regex "/path/to/dir/", // not a valid regex "[integer]i", // valid regex not implementing a character class ]; $input_string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer quam ex, vestibulum sed laoreet auctor, iaculis eget velit. Donec mattis, /path/to/dir/ nulla ac suscipit maximus, leo metus vestibulum eros, nec finibus nisl dui ut est. Nam tristique varius mauris, a faucibus augue."; $result = []; foreach($list as $v) { if (@preg_match($v, '') === false) { // not a regex, make into one $v = '/' . preg_quote($v, '/') . '/'; } preg_match($v, $input_string, $m); $result[$v] = $m[0] ?? null; } var_export($result);
You have javascript disabled. You will not be able to edit any code.