diff --git a/emoji_scrub.module b/emoji_scrub.module
index 9e23cfc..eb25fe4 100644
--- a/emoji_scrub.module
+++ b/emoji_scrub.module
@@ -148,14 +148,107 @@ function _emoji_scrub_adub_dub($text, $replace = '') {
   $regex .= '|([\xC0\xC1]|[\xF0-\xFF])[\x80-\xBF]*';
   $regex .= '|[\xC2-\xDF]((?![\x80-\xBF])|[\x80-\xBF]{2,})';
   $regex .= '|[\xE0-\xEF](([\x80-\xBF](?![\x80-\xBF]))|(?![\x80-\xBF]{2})|[\x80-\xBF]{3,})/S';
-  $stripped_string = preg_replace($regex, $replace, $text);
+  $stripped_string = preg_replace_callback(
+    $regex, 
+     function ($matches) {
+        $code = utf8_ord($matches[0]);
+        if (!$code) {
+          return '';
+        } else {
+          return '&#' . $code . ';';
+        };
+    },
+   $text);
   // Reject overly long 3 byte sequences and UTF-16 surrogates and replace
   // with --.
-  $stripped_string = preg_replace('/\xE0[\x80-\x9F][\x80-\xBF]' . '|\xED[\xA0-\xBF][\x80-\xBF]/S', $replace, $stripped_string);
+  $stripped_string = preg_replace_callback(
+    '/\xE0[\x80-\x9F][\x80-\xBF]' . '|\xED[\xA0-\xBF][\x80-\xBF]/S', 
+     function ($matches) {
+          $code = utf8_ord($matches[0]);
+          if (!$code) {
+            return '';
+          } else {
+            return '&#' . $code . ';';
+          };
+      },
+     $stripped_string);
   return $stripped_string;
 }
 
 /**
+ * UTF-8 aware alternative to ord.
+ *
+ * Returns the unicode ordinal for a character or FALSE if not valid
+ *
+ * @see http://www.php.net/manual/en/function.ord.php#46267
+ * @param string $chr UTF-8 encoded character
+ * @return int unicode ordinal for the character
+ * @package php-utf8
+ * @subpackage functions
+ */
+
+function utf8_ord($chr)
+{
+  $ord0 = ord($chr);
+  if ($ord0 >= 0 && $ord0 <= 127)
+
+    return $ord0;
+
+  if (!isset($chr[1]))
+  {
+    return false;
+  }
+
+  $ord1 = ord($chr[1]);
+
+  if ($ord0 >= 192 && $ord0 <= 223)
+    return ($ord0 - 192) * 64 + ($ord1 - 128);
+
+  if (!isset($chr[2]))
+  {
+    return false;
+  }
+
+  $ord2 = ord($chr[2]);
+
+  if ($ord0 >= 224 && $ord0 <= 239)
+    return ($ord0 - 224) * 4096 + ($ord1 - 128) * 64 + ($ord2 - 128);
+
+  if (!isset($chr[3]))
+  {
+    return false;
+  }
+
+  $ord3 = ord($chr[3]);
+
+  if ($ord0 >= 240 && $ord0 <= 247)
+    return ($ord0 - 240) * 262144 + ($ord1 - 128) * 4096 + ($ord2 - 128) * 64 + ($ord3 - 128);
+
+  if (!isset($chr[4]))
+  {
+    return false;
+  }
+
+  $ord4 = ord($chr[4]);
+
+  if ($ord0 >= 248 && $ord0 <= 251)
+    return ($ord0 - 248) * 16777216 + ($ord1 - 128) * 262144 + ($ord2 - 128) * 4096 + ($ord3 - 128) * 64 + ($ord4 - 128);
+
+  if (!isset($chr[5]))
+  {
+    return false;
+  }
+
+  if ($ord0 >= 252 && $ord0 <= 253)
+    return ($ord0 - 252) * 1073741824 + ($ord1 - 128) * 16777216 + ($ord2 - 128) * 262144 + ($ord3 - 128) * 4096 + ($ord4 - 128) * 64 + (ord($chr[5]) - 128);
+
+  if ($ord0 >= 254 && $ord0 <= 255)
+  {
+    return false;
+  }
+}
+
+/**
  * Implements hook_permission().
  */
 function emoji_scrub_permission() {
