htmlentities不适用于json编码

时间:2013-02-01 02:46:21

标签: php mysqli

我不知道这是怎么回事,但是htmlentities()函数不能正常工作!

这就是我所做的

(底部代码工作正常,但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);

1 个答案:

答案 0 :(得分:5)

传递给json_encode()的值必须是UTF-8编码数据。 因此,在运行json_encode之前,请使用utf8_encode

示例:

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