[英]Converting Newlines in JSON array to line breaks: JSON_decode and nl2br
我无法获得以下数组以用 HTML 中断替换回车符和换行符<br />
JSON 数组:
{"水果":"苹果","蔬菜":"胡萝卜\\r\\n芹菜\\r\\n\\r\\n黄瓜"}
我正在尝试这个并且没有运气:
$html = json_decode(nl2br($json),true);
换行符在 HTML 本身的源代码中可见,但是我无法获得<br />
代码来替换“换行符”。
我也尝试过这个建议,但它也不起作用: 运行 json_encode 后替换 \\r\\n (换行符)
在 JSON 解码后运行nl2br()
,而不是在 JSON 数组源上运行。
也许你应该试试
$arJson = array( 'fruit' => 'apple',
'veggies' => array( 'carrot', 'celery', 'cucumbers' ) );
header( 'Content-Type: application/json' );
print json_encode( $arJson );
返回一个 JSON
然后使用 jQuery 或您最喜欢的库(甚至是自定义构建库)向页面发送 ajax 请求。
$.ajax({
url: 'frtsnvgs.php',
dataType: 'JSON',
success: function( oData ) {
$.each( oData, function( key, value ) {
document.write( key + ': ' + value + '<br>' );
});
}
});
试试这个代码。 这可能会帮助你
$json = '{"Fruit":"Apple","Veggies":"Carrot\r\nCelery\r\n\r\nCucumbers"}';
$json = json_decode($json,true);
array_walk($json, function(&$val){$val = html_entity_decode(nl2br($val));});
echo "<pre>";
print_r($json);
echo "</pre>";
exit;
用
$html = json_decode(nl2br($json,false),true);
语法: nl2br(string,xhtml)
与: xhtml
可选。 一个布尔值,指示是否使用兼容 XHTML 的换行符:
TRUE
- 默认。 插入<br />
FALSE
- 插入<br>
使用<br />
它会使 json 错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.