utf8_encode and Emoji

2019-09-10 02:50发布

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

1条回答
啃猪蹄的小仙女
2楼-- · 2019-09-10 03:19

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']);
查看更多
登录 后发表回答