English 中文(简体)
json_encode(): Invalid UTF-8
原标题:json_encode(): Invalid UTF-8 sequence in argument

I m calling json_encode() on data that comes from a MySQL database with utf8_general_ci collation. The problem is that some rows have weird data which I can t clean. For example symbol , so once it reaches json_encode(), it fails with json_encode(): Invalid UTF-8 sequence in argument.

I ve Trial utf8_encode(> and utf8_decode(<>/code>, 即便有mb_check_encoding(<>,但通过并造成严重破坏。

Running PHP 5.3.10 on Mac. So the question is - how can I clean up invalid utf8 symbols, keeping the rest of data, so that json_encoding() would work?

更新。 这里可以复制:

echo json_encode(pack("H*" , c32e ));
最佳回答

如同文号一样,见A,但由于数据包括姓名,因此,只有一封信被显示,最后名称是(0],这对多彩的扼杀是错误的,造成整个 has。 改为mb_substr ($lastname, 0, 1)——与一家药厂一样。

问题回答

我有类似的错误,使json_encode在具有血缘特征时,如在地狱中出现曲线的外向,即退回一个无效的田地,因为地盘中将出现错误的特性。

解决办法是通过增加:

mysql_set_charset( utf8 );

在我sql接线发言之后。

The problem is that this character is UTF8, but json_encode does not handle it correctly. To say more, there is a list of other characters (see Unicode characters list), that will trigger the same error, so stripping off this one (Å) will not correct an issue to the end.

我们使用的是将这些果园转化为像以下的html实体:

htmlentities( (string) $value, ENT_QUOTES,  utf-8 , FALSE);

确保你与我的SQL的联系是UTF-8。 常常没有ISO-8859-1,这意味着MySQL的驾驶员将把该文本改为ISO-8859-1。

http://php.net/mysql_set_charset”rel=“noretinger”>mysql_set_charset,

采用这一守则可能有助于。 它解决了我的问题!

mb_convert_encoding($post["post"], UTF-8 , UTF-8 );

或如

mb_convert_encoding($string, UTF-8 , UTF-8 );

您的文号是按顺序细分的持有人符号。 基本上,它不是真正的象征,而是你所描述的错误。

象征的确切直线价值是什么? 盲人使用utf8_encode不是一个好主意,最好先发现一下底部来自什么地方,以及它们意味着什么。

造成这种错误的另一个情况是,当你使用S. . json_encode功能时,U类编码是上下级,而不是下级。

json_en code只使用UTF-8数据。 你必须确保你的数据在UTF-8中。 或者,你可以使用iconv()将成果转换到UTF-8,然后把结果传给json_en code()。

更新。 我在解决这一问题时指出,PDO连接的果园如下:

“mysql:host=$host;dbname=$db;charset=utf8”

其后收到的所有数据均在《刑法》其余部分使用的正确标题中。

I am very late but if some one working on SLIM to make rest api and getting same error can solve this problem by adding below line as:

<?php

// DbConnect.php file
class DbConnect
{
    //Variable to store database link
    private $con;

    //Class constructor
    function __construct()
    {

    }

    //This method will connect to the database
    function connect()
    {
        //Including the constants.php file to get the database constants
        include_once dirname(__FILE__) .  /Constants.php ;

        //connecting to mysql database
        $this->con = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME);

        mysqli_set_charset($this->con, "utf8"); // add this line 
        //Checking if any error occured while connecting
        if (mysqli_connect_errno()) {
            echo "Failed to connect to MySQL: " . mysqli_connect_error();
        }

        //finally returning the connection link
        return $this->con;
    }
}

在json_encode之前使用当地语解决了这一问题。





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...