目前MySQL db中的一个字段包含以下内容
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。
,我没关系答案 0 :(得分:1)
您可以使用<br>
替换PHP中所讨论的列的所有换行符,因为当它到达这样的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;
}