Try the following:
<?php
$input_string = "assasins: 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.";
$list = [ // an array list of string/regex that i want to check
"ass", // should match the ass in assasins
"Lorem ipsum", // a words
"consectetur", // another word
"/(nu[a-z]{2}a)/", // a regex
];
$regex_list = [];
foreach($list as $line) {
if ($line[0] == '/' and $line[-1] == '/')
$regex = '(?:' . substr($line, 1, -1) . ')';
else
$regex = '\\b' . preg_quote($line, $delimiter='/') . '\\b';
$regex_list[] = $regex;
}
$regex = '/' . implode('|', $regex_list) . '/';
echo "$regex\n";
preg_match_all($regex, $input_string, $matches, PREG_SET_ORDER);
print_r($matches);
$s = [];
foreach ($matches as &$match) {
$s[] = $match[0];
}
$s = json_encode($s);
echo "Matched strings: ", substr($s, 1, -1), "\n";
Prints:
/\bass\b|\bLorem ipsum\b|\bconsectetur\b|(?:(nu[a-z]{2}a))/
Array
(
[0] => Array
(
[0] => Lorem ipsum
)
[1] => Array
(
[0] => consectetur
)
[2] => Array
(
[0] => nulla
[1] => nulla
)
)
Matched strings: "Lorem ipsum","consectetur","nulla"
Discussion and Limitations
In processing each element of $list
, if the string begins and ends with '/', it is assumed to be a regular expression and the '/' characters are removed from the start and end of the string. Therefore, anything else that does not begin and end with these characters must be a plain string. This implies that if the OP wanted to match a plain string that just happens to begin and end with '/', e.g. '/./', they would have to do it instead as a regular expression: '/\/.\//'. A plain string is replaced by the results of calling preg_quote
on it to escape special characters that have meaning in regular expressions thus converting it into a regex without the opening and closing '/' delimiters. Finally, all the strings are joined together with the regular expression or character, '|', and then prepended and appended with '/' characters to create a single regular expression from the input.
The main limitation is that this does not automatically adjust backreference numbers if multiple regular expressions in the input list have capture groups, since the group numberings will be effected when the regular expressions are combined. Therefore such regex patterns must be cognizant of prior regex patterns that have capture groups and adjust its backreferences accordingly (see demo below).
Regex flags (i.e. pattern modifiers) must be embedded within the regex itself. Since such flags in one regex string of $list
will effect the processing of another regex string, if flags are used in one regex that do not apply to a subsequent regex, then the flags must be specifically turned off:
<?php
$input_string = "This is an example by Booboo.";
$list = [ // an array list of string/regex that i want to check
"/(?i)booboo/", // case insensitive
"/(?-i)EXAMPLE/" // explicitly not case sensitive
];
$regex_list = [];
foreach($list as $line) {
if ($line[0] == '/' and $line[-1] == '/')
$regex_list[] = substr($line, 1, -1);
else
$regex_list[] = preg_quote($line, $delimiter='/');
}
$regex = '/' . implode('|', $regex_list) . '/';
echo $regex, "\n";
preg_match_all($regex, $input_string, $matches, PREG_SET_ORDER);
print_r($matches);
$s = [];
foreach ($matches as &$match) {
$s[] = $match[0];
}
$s = json_encode($s);
echo "Matched strings: ", substr($s, 1, -1), "\n";
Prints:
/(?i)booboo|(?-i)EXAMPLE/
Array
(
[0] => Array
(
[0] => Booboo
)
)
Matched strings: "Booboo"
This shows how to correctly handle backreferences by manually adjusting the group numbers:
<?php
$input_string = "This is the 22nd example by Booboo.";
$list = [ // an array list of string/regex that i want to check
"/([0-9])\\1/", // two consecutive identical digits
"/(?i)([a-z])\\2/" // two consecutive identical alphas
];
$regex_list = [];
foreach($list as $line) {
if ($line[0] == '/' and $line[-1] == '/')
$regex_list[] = substr($line, 1, -1);
else
$regex_list[] = preg_quote($line, $delimiter='/');
}
$regex = '/' . implode('|', $regex_list) . '/';
echo $regex, "\n";
preg_match_all($regex, $input_string, $matches, PREG_SET_ORDER);
print_r($matches);
$s = [];
foreach ($matches as &$match) {
$s[] = $match[0];
}
$s = json_encode($s);
echo "Matched strings: ", substr($s, 1, -1), "\n";
Prints:
/([0-9])\1|(?i)([a-z])\2/
Array
(
[0] => Array
(
[0] => 22
[1] => 2
)
[1] => Array
(
[0] => oo
[1] =>
[2] => o
)
[2] => Array
(
[0] => oo
[1] =>
[2] => o
)
)
Matched strings: "22","oo","oo"