aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Lange <code@nerdmind.de>2021-07-16 23:50:07 +0200
committerThomas Lange <code@nerdmind.de>2021-07-16 23:57:04 +0200
commit89e2ab9b872f34361bed8274f3d90cda6abddced (patch)
tree17ad8399982158012ccde657be0de3362032ec06
parentea00b92232d2efb121e55825aef7dfa28b97349e (diff)
downloadblog-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.
-rw-r--r--core/functions.php18
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);
}
#===============================================================================