当前MySQL数据库中的一个字段包含如下
PARTICIPATE
These are a few areas which would greatly improve with your participation
有两条新线。
然而,当从Json中提取时,它变成了下面的内容,其中缺少新行。
{Description: "PARTICIPATE These are a few areas which would greatly improve with your participation"}
我的PHP代码如下
function queryPrintJson($cnx, $query) {
$rows=queryReturnJsonArray($cnx, $query);
print json_encode($rows);
}
function queryReturnJsonArray($cnx, $query) {
$result=mysqli_query($cnx, $query) or die ("Can't execute query!");
$rows = array();
while($obj = $result->fetch_object()){
$rows[] = $obj;
}
$result->close();
return $rows;
}
有没有我可以保留换行符?如果它被转换成''''n,我没问题。
您可以将PHP中有问题的列的所有换行符替换为<br>
,因为当它使用这样的javascript代码时,它最终将成为HTML。
function queryReturnJsonArray($cnx, $query) {
$result=mysqli_query($cnx, $query) or die ("Can't execute query!");
$rows = array();
while($obj = $result->fetch_object()){
$obj->col_name = str_replace("'n", '<br>', $obj->col_name);
$rows[] = $obj;
}
$result->close();
return $rows;
}