本文介紹了php json_encode() 顯示 null 而不是文本的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
限時送ChatGPT賬號..
我正在從數據庫中讀取一些希伯來語文本并嘗試 json_encode
它.如果我 print_r
我得到的結果:
I am reading from database with some text in Hebrew and trying to json_encode
it.
if i print_r
the results i get:
Array
(
[0] => Array
(
[value] => 88
[text] => ???? ?'
[parent_id] => 1
[level] => 1
)
[1] => Array
(
[value] => 89
[text] => ???? ?'
[parent_id] => 1
[level] => 1
)
[2] => Array
(
[value] => 91
[text] => ???? ?'
[parent_id] => 1
[level] => 1
)
)
雖然 json_encode 顯示:
while the json_encode shows:
[{"value":"88","text":null,"parent_id":"1","level":"1"},{"value":"89","text":null,"parent_id":"1","level":"1"},{"value":"91","text":null,"parent_id":"1","level":"1"}]
我相信這是因為我的數據庫中的文本包含 ( ' ) 標記.嘗試了各種stripslashes或real_escape_string的組合都沒有幫助.
i belive it's because my text from the database contains a ( ' ) mark. tried various combination of stripslashes or real_escape_string none have helped.
推薦答案
json_encode
期望數據中的字符串被編碼為 UTF-8.
json_encode
expects strings in the data to be encoded as UTF-8.
如果尚未將它們轉換為 UTF-8:
Convert them to UTF-8 if they aren't already:
$results = array_map(function($r) {
$r['text'] = utf8_encode($r['text']);
return $r;
}, $results);
echo json_encode($results);
這篇關于php json_encode() 顯示 null 而不是文本的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!