Browse code

decode unicode chars in cleverbot response (PHP)

Pierre-David BeĢlanger authored on 19/07/2015 02:56:18
Showing 1 changed files
... ...
@@ -188,7 +188,21 @@
188 188
             $this->vars['typingData'] = _utils_string_at_index($responseValues, 22);
189 189
             $this->vars['divert'] = _utils_string_at_index($responseValues, 23);
190 190
             $responseThought = new ChatterBotThought();
191
-            $responseThought->setText(_utils_string_at_index($responseValues, 16));
191
+            $text = _utils_string_at_index($responseValues, 16);
192
+            if (!is_null($text))
193
+            {
194
+                $text = preg_replace_callback(
195
+                    '/\|([01234567890ABCDEF]{4})/', 
196
+                    function ($matches) { 
197
+                        return iconv('UCS-4LE', 'UTF-8', pack('V', hexdec($matches[0]))); 
198
+                    }, 
199
+                    $text);
200
+            } 
201
+            else
202
+            {
203
+                $text = '';
204
+            }
205
+            $responseThought->setText($text);
192 206
             return $responseThought;
193 207
         }
194 208
     }