Don't replace smileys/emojis within words

Steffen

Well-known member
For example, writing host:port currently results in :p being replaced by the "stick out tongue" smiley: host:port

Ideally, XenForo should detect that in this case the user probably didn't intend :p to be a smiley. It could check whether potential smilies whose "Text to replace" is not delimited by colons are surrounded by a word boundary.

Proof of concept:
Diff:
--- a/src/XF/Str/Formatter.php
+++ b/src/XF/Str/Formatter.php
@@ -175,7 +175,15 @@ class Formatter
     {
         if ($this->smilieTranslate)
         {
-            $text = strtr($text, $this->smilieTranslate);
+            $pattern = '/(' . implode('|', array_map(function($str) {
+                $prefix = ctype_alnum($str[0]) ? '\b' : '';
+                $suffix = ctype_alnum($str[-1]) ? '\b' : '';
+                return $prefix . preg_quote($str, '/') . $suffix;
+            }, array_keys($this->smilieTranslate))) . ')/';
+
+            $text = preg_replace_callback($pattern, function($m) {
+                return $this->smilieTranslate[$m[1]];
+            }, $text);
         }

         if ($escapeCallback)


This suggestion originally was a bug report: https://xenforo.com/community/threads/prevent-smileys-emojis-to-be-replaced-within-words.163896/
 
Last edited:
Upvote 11
Top Bottom