I'm using this code
preg_match_all("/([^#]+\btbds\b.+?)#/iu", $data, $matches);
to find all words named tbds, but its taking around 1.20 seconds to perform the pattern search. If I just use tbds\b instead of \btbds\b it takes just 0.19 seconds (6 times less).
preg_match_all("/([^#]+tbds\b.+?)#/iu", $data, $matches);
is there any way to optimize the word match \btbds\b to take around 0.19 seconds? I need to process a large amount of data.
here is the test code:
function generateRandomString($length = 10) {
$characters = ' 0123 456 789 abcd efgh ijkl mn opqrstu vwx yzAB CDE FGHI JKL MNOP QRS TUVWX YZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
$randomString = preg_replace('/\s+/', ' ', $randomString);
return trim($randomString,' ');
}
$data=NULL;
for ($a = 1; $a < 1000000; $a++)
$data.=" ".generateRandomString(100)." #";
$t = microtime(true);
preg_match_all("/([^#]+\btbds\b.+?)#/iu", $data, $matches);
echo microtime(true) - $t; echo "\n";
\bwill happily matchatbds #while the other one will not$res = preg_grep('/\btbds\b/i', explode("#", $data));#symbols generated. If you want us to design an optimized pattern for you, we need to fully understand the input variability. Furthermore, you are performing unicode matching, but there are no unicode characters on offer. Please improve your question.