diff options
author | Thomas Lange <code@nerdmind.de> | 2021-07-16 23:50:07 +0200 |
---|---|---|
committer | Thomas Lange <code@nerdmind.de> | 2021-07-16 23:57:04 +0200 |
commit | 89e2ab9b872f34361bed8274f3d90cda6abddced (patch) | |
tree | 17ad8399982158012ccde657be0de3362032ec06 /core | |
parent | ea00b92232d2efb121e55825aef7dfa28b97349e (diff) | |
download | blog-89e2ab9b872f34361bed8274f3d90cda6abddced.tar.gz blog-89e2ab9b872f34361bed8274f3d90cda6abddced.tar.xz blog-89e2ab9b872f34361bed8274f3d90cda6abddced.zip |
Improve performance of parseUnicodeEmoticons
Make the function parseUnicodeEmoticons significantly faster by using
a single regex operation to match and process all unicode emoticons.
Diffstat (limited to 'core')
-rw-r--r-- | core/functions.php | 18 |
1 files changed, 10 insertions, 8 deletions
diff --git a/core/functions.php b/core/functions.php index a9d103e..b622601 100644 --- a/core/functions.php +++ b/core/functions.php @@ -306,14 +306,16 @@ function getUnicodeEmoticons(): array { # Wrap emoticons in <span> element with "title" attribute for explanation #=============================================================================== function parseUnicodeEmoticons($string): string { - foreach(getUnicodeEmoticons() as $emoticon => $explanation) { - $pattern = '#(^|\s)'.preg_quote($emoticon).'#'; - $replace = "\\1<span title=\"{$explanation}\">{$emoticon}</span>"; - - $string = preg_replace($pattern, $replace, $string); - } - - return $string; + $emoticon_data = getUnicodeEmoticons(); + $emoticon_list = array_keys($emoticon_data); + $emoticon_list = implode('|', $emoticon_list); + + return preg_replace_callback("#($emoticon_list)#", function($matches) + use($emoticon_data) { + $emoticon = $matches[1]; + $explanation = $emoticon_data[$emoticon]; + return sprintf('<span title="%s">%s</span>', $explanation, $emoticon); + }, $string); } #=============================================================================== |