Skip to content

Commit

Permalink
Optimize by calling preg_replace_callback() only once
Browse files Browse the repository at this point in the history
  • Loading branch information
vlakoff committed Sep 25, 2021
1 parent b0f5148 commit 4c49b17
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions system/Database/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -406,11 +406,15 @@ public function debugToolbarDisplay(): string

$sql = $this->finalQueryString;

foreach ($highlight as $term) {
$sql = preg_replace_callback('/\b' . preg_quote($term, '/') . '\b/', static function ($matches) {
return '<strong>' . str_replace(' ', '&nbsp;', $matches[0]) . '</strong>';
}, $sql);
}
$escapedTerms = array_map(static function ($term) {
return preg_quote($term, '/');
}, $highlight);

$search = '/\b(?:' . implode('|', $escapedTerms) . ')\b/';

$sql = preg_replace_callback($search, static function ($matches) {
return '<strong>' . str_replace(' ', '&nbsp;', $matches[0]) . '</strong>';
}, $sql);

return $sql;
}
Expand Down

0 comments on commit 4c49b17

Please sign in to comment.