From 89e2ab9b872f34361bed8274f3d90cda6abddced Mon Sep 17 00:00:00 2001 From: Thomas Lange Date: Fri, 16 Jul 2021 23:50:07 +0200 Subject: Improve performance of parseUnicodeEmoticons Make the function parseUnicodeEmoticons significantly faster by using a single regex operation to match and process all unicode emoticons. --- core/functions.php | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) (limited to 'core') 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 element with "title" attribute for explanation #=============================================================================== function parseUnicodeEmoticons($string): string { - foreach(getUnicodeEmoticons() as $emoticon => $explanation) { - $pattern = '#(^|\s)'.preg_quote($emoticon).'#'; - $replace = "\\1{$emoticon}"; - - $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('%s', $explanation, $emoticon); + }, $string); } #=============================================================================== -- cgit v1.2.3