Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
+8 votes
1.1k views
in Q2A Core by

Questions or answers that include an Emoji character (standard unicode) fail to save correctly.

Is this a known bug that is looking to be resolved?

For instance, a question that features an Emoji in it, will only save the text before the Emoji character. Text after it is lost on submission.

To reproduce:-

  • Use Safari or Firefox (Mac) or Internet Explorer 10 or latest Firefox (Windows 8)
  • Cannot use Chrome - it doesn't yet support Emoji
  • Ask a question that includes an Emoji character - can copy and paste the character from http://getemoji.com
  • Submit question. Only text before the Emoji character is saved.
Q2A version: 1.6.2
by
+1
@gidgreen provided a thorough answer below but there are two approaches for offering emoji support: The full-fledged solution described in @gidgreen’s answer, and an alternative fallback adjustment that uses HTML entities for adding partial support.  More details about these approaches can be found by the end of this comment:
https://www.question2answer.org/qa/54525/is-there-a-way-to-get-unicode-smiley-work-with-q2a?show=91504#c91504

1 Answer

+6 votes
by

This is actually a problem occurring at the MySQL level, since the regular MySQL UTF-8 character set does not support 4-byte UTF-8 characters, of which Emoji are an example.

If you are using MySQL 5.5.3 or later, there's a character set called utf8mb4 which can be substituted for utf8, and which allows Emoji. In order to switch to this character set you need to:

  • Change the encodings in qa_db_connect(...) in qa-db.php from utf8 to utf8mb4 (two places).
  • Change the character set of columns qa_posts.content (for post content), qa_words.word (for indexing), qa_users.handle (for searching), qa_userprofile.content (for profile pages), qa_messages.content (for private message / wall posts) to utf8mb4.
  • Or if you're creating a new Q2A installation you can skip the previous step and just change utf8 to utf8mb4 in qa_db_create_table_sql(...) in qa-db-install.php before installing.

I'm not sure if it makes sense to include this in the Q2A core since the utf8mb4 character set is only supported since MySQL 5.5.3, and can take up more space in some cases.

by
Add-on to work on newer versions.
After doing the above procedure, edit the file qa-include/util/string.php

Search for:

function qa_remove_utf8mb4($string)
{


and add the line: return $string;
at the beginning of the function

it should look like this:

function qa_remove_utf8mb4($string)
{
return $string;


See how it should look below:

BEFORE:
function qa_remove_utf8mb4($string)
{
    return preg_replace('%(?:
          \xF0[\x90-\xBF][\x80-\xBF]{2}  # planes 1-3
        | [\xF1-\xF3][\x80-\xBF]{3}      # planes 4-15
        | \xF4[\x80-\x8F][\x80-\xBF]{2}  # plane 16
        )%xs', '', $string);
}


AFTER:
function qa_remove_utf8mb4($string)
{
return $string;
    return preg_replace('%(?:
          \xF0[\x90-\xBF][\x80-\xBF]{2}  # planes 1-3
        | [\xF1-\xF3][\x80-\xBF]{3}      # planes 4-15
        | \xF4[\x80-\x8F][\x80-\xBF]{2}  # plane 16
        )%xs', '', $string);
}

Okay, now test sending emojis hahaha, the emoji will be enabled in any area of the site. See the before and after changes on my website:

Before, after doing just the procedure of the answer above: https://i.imgur.com/dMHzuRW.jpg

After doing the above answer procedure and the procedure in the file: qa-include/util/string.php -> https://i.imgur.com/QKxj3DA.png e https://i.imgur.com/A95GrzZ.png

I hope it helps!
by
I already try this , but it got this error when I visit tag page

A Question2Answer database query failed when generating this page.

A full description of the failure is available in the web server's error log file.

is there anybody can help me ?
...