Function build_regex
Creates optimized regular expressions from an array of strings.
An optimized regex built using this function will be much faster than a
simple regex built using implode('|', $strings)
--- anywhere from several
times to several orders of magnitude faster.
However, the time required to build the optimized regex is approximately equal to the time it takes to execute the simple regex. Therefore, it is only worth calling this function if the resulting regex will be used more than once.
Because PHP places an upper limit on the allowed length of a regex, very large arrays of $strings may not fit in a single regex. Normally, the excess strings will simply be dropped. However, if the $returnArray parameter is set to true, this function will build as many regexes as necessary to accommodate everything in $strings and return them in an array. You will need to iterate through all elements of the returned array in order to test all possible matches.
Copyright: 2020 Simple Machines and individual contributors
License: BSD
Author: Simple Machines https://www.simplemachines.org
Located at Sources/Subs.php
array |
$strings |
An array of strings to make a regex for. |
string |
$delim = null |
An optional delimiter character to pass to preg_quote(). |
boolean |
$returnArray = false |
If true, returns an array of regexes. |
string|array
|
One or more regular expressions to match any of the input strings. |