简体   繁体   English

htmlentities无法使用json编码

[英]htmlentities is not working with json encoding

I don't know what is going on with this but htmlentities() function is not working! 我不知道这是怎么回事,但是htmlentities()函数不起作用!

Here is what i have done 这是我所做的

(the bottom code work correctly but htmlentities() does not convert quotes to html code! for example " should be change to some html5 code! (底部代码可以正常工作,但是htmlentities()不能将引号转换为html代码!例如,“应该更改为某些html5代码!

$term = "'" . addslashes($_GET['search_term']) . "%'";
if(!isset($term{2})){
    exit();
}

$query = $db->query('SELECT customerID, fullName, dID, birthYear, homeAddress, ID, DATE_FORMAT(idIssue, "%d-%m-%Y") AS idIssue, DATE_FORMAT(idExp, "%d-%m-%Y") AS idExp, phone
                     FROM customers WHERE (fullName LIKE '.$term.' ) LIMIT 0,10');



while( $row = $query->fetch_assoc()){

    foreach($row as $key => $value){
        $arr[$key] = htmlentities(stripslashes($value), ENT_QUOTES);
    }
    $json[] = $arr;
}

echo json_encode($json);

The value you pass to json_encode() must be UTF-8 encoded data. 您传递给json_encode()的值必须是UTF-8编码的数据。 Therefore, before you run json_encode , use utf8_encode . 因此,在运行json_encode之前,请使用utf8_encode

Example: 例:

 $arr[$key] = htmlentities(stripslashes(utf8_encode($value)), ENT_QUOTES);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM