0

I have a problem utf8 encoding. In the wordpress database, there are many emojis but when I encode, they no longer appear. There is a "?" instead that appears.

Can you help me ? I think it comes from utf8_encode

here is the code :

$results = $connection->query($req) or die(Array());

$results->setFetchMode(PDO::FETCH_OBJ); 

$i = 0;
$jsonArray = Array();
while($row = $results->fetch()) {   

    $jsonArray[$i][0] = utf8_encode($row->comment_author);
    $jsonArray[$i][1] = utf8_encode(nl2br($row->comment_content));
    $jsonArray[$i][2] = utf8_encode($row->comment_date);
    $jsonArray[$i][3] = utf8_encode($row->replyingToAuthor);
    $jsonArray[$i][4] = utf8_encode($row->comment_ID);

    ++$i;
}

$results->closeCursor();
$connection = NULL;
echo json_encode($jsonArray);

This is the line that displays the comment:

 $jsonArray[$i][1] = utf8_encode(nl2br($row->comment_content));

I have no problem with encoding accents, only with emoji

Thank you

dibs
  • 174
  • 1
  • 12
  • Please post a clear sample of the **input** and desired **output**. – Pedro Lobito May 13 '16 at 20:36
  • 2
    If you want utf8 with emojis, I believe you need to specify utf8mb4 as the charset wherever possible. And if you control the inputs, you don't need to `utf8_encode` your variables, just set all the charsets to utf8 (or utf8mb4 where you can). (Connection, file, PHP + HTML header, database, tables, etc) – Qirel May 13 '16 at 20:44
  • What do you get if you don't call `utf8_encode()` on the data? – trincot May 13 '16 at 20:46
  • 1
    Possible duplicate of [UTF-8 all the way through](http://stackoverflow.com/questions/279170/utf-8-all-the-way-through) – Charlotte Dunois May 13 '16 at 21:33
  • if I put `nl2br($row->comment_content);` instead of `utf8_encode(nl2br($row->comment_content));` I "NULL" that appears in place of the content – dibs May 13 '16 at 22:12
  • @dibs Can you `var_dump($row->comment_content);` and post the result here? I find it odd that you get NULL if you remove the encoding, as that should only result in broken letters. – Qirel May 13 '16 at 22:40

2 Answers2

0

This is what I did to solve this:

Create a function

function accents($texto) {
    $text = nl2br($text); // allows to have spaces in the text
    $text = utf8_encode($text); //allows to have accents and special characters
    return $text;
}

Call the function

echo formatoAcentos($post['texto']);
Alexander Farber
  • 21,519
  • 75
  • 241
  • 416
CrisPM
  • 1
0

I improve that, because it didnt't work.

Function:

function formatoAcentos($texto) {
$nvotexto = nl2br($texto);          // permite mantener el salto de línea
$nvotexto = utf8_encode($nvotexto); // permite caracteres especiales como los acentos
return $nvotexto;
}

Function call.

echo formatoAcentos($row['descripcion_doc'])