I have a list of string/regex that I want to check if its matched from the string input.
Lets just say I have these lists:
$list = [ // an array list of string/regex that i want to check
"lorem ipsum", // a words
"example", // another word
"/(nulla)/", // a regex
];
And the string:
$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.";
And so, I want it to check like this:
if( $matched_string >= 1 ){ // check if there was more than 1 string matched or something...
// do something...
// output matched string: "lorem ipsum", "nulla"
}else{
// nothing matched
}
How can I do something like that?
3
Answers
I’m not sure if this approach would work for your case but, you could treat them all like regexes.
The above will output the following:
Sandbox
Try the following:
Prints:
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 callingpreg_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:Prints:
This shows how to correctly handle backreferences by manually adjusting the group numbers:
Prints:
Typically, I scream bloody murder if someone dares to stink up their code with error suppressors. If your input data is so out-of-your-control that you are allowing a mix of regex an non-regex input strings, then I guess you’ll probably condone
@
in your code as well.Validate the search string to be regex or not as demonstrated here. If it is not a valid regex, then wrap it in delimiters and call
preg_quote()
to form a valid regex pattern before passing it to the actual haystack string.Code: (Demo)
Or you could write the same thing this way, but I don’t know if there is any drag in performance by checking the pattern against a non-empty string: (Demo)