* json_encode返回 NULL? 不是答案。我在使用 json_encode 时仍然收到空。
我对PHP很陌生,所以如果你能用固定的代码编辑这个部分,我将不胜感激。
这是我的问题:
当"内向文本"下的文章包含非中断行时,它将返回 NULL。没有不间断空格的文章显示得很好。
这是我的问题:
如何让"introtext"下的文章正确显示,即使它们包含不间断的空格。
代码如下:
$connection = mysqli_connect($host, $user, $pass);
//Check to see if we can connect to the server
if(!$connection)
{
die("Database server connection failed.");
}else{
//Attempt to select the database
$dbconnect = mysqli_select_db($connection, $db);
//Check to see if we could select the database
if(!$dbconnect)
{
die("Unable to connect to the specified database!");
}else{
$catID = $_GET['catid'];
$id = $_GET['id'];
$rtn = $_GET['rtn'];
if($id!=""){
$query = "SELECT * FROM tcp_content WHERE id=" . $id . "";
}else{
$query = "SELECT * FROM tcp_content WHERE catid=" . $catID . " ORDER BY publish_up DESC LIMIT " . $rtn . "";
}
$resultset = mysqli_query($connection,$query);
$records = array();
//Loop through all records and add them to array
while($r = mysqli_fetch_assoc($resultset))
{
$r['introtext'] = print_r($r['introtext'],true);
$records[] = $r;
}
//Output the data as JSON
echo json_encode($records);
}
}
?>
这里有两个链接:
此链接包含不间断空格,因此您会注意到内联文本返回 NULL
此链接不包含不间断空格,因此您会注意到文章显示
我发现这个链接json_encode问题
见第二个答案。查尔斯建议使用iconv()
删除URL编码的不间断空格。
我终于想通了,让它工作起来
$r['introtext'] = utf8_encode($r['introtext']);
$r['introtext'] = str_replace(chr(194).chr(160),' ',$r['introtext']);
$r['introtext'] = str_replace(chr(194).chr(147),'"',$r['introtext']);
$r['introtext'] = str_replace(chr(194).chr(148),'"',$r['introtext']);
$r['introtext'] = str_replace(chr(194).chr(146),"'",$r['introtext']);
$r['introtext'] = str_replace(chr(194).chr(145),"'",$r['introtext']);
$r['introtext'] = htmlentities($r['introtext'], ENT_QUOTES | ENT_IGNORE, "UTF-8");
$records = $r;