Php json解码删除新行
查找我的Json代码Php json解码删除新行,php,json,decode,Php,Json,Decode,查找我的Json代码 $result = { "text_1": [ { "text": "Name \nKarSho", "left": 0, "right": 0 } ] } 我想要这个Json的PHP文本。我用了下面的脚本
$result = {
"text_1":
[
{
"text": "Name \nKarSho",
"left": 0,
"right": 0
}
]
}
我想要这个Json的PHP文本。我用了下面的脚本
$json = json_decode($result,true);
if($json && isset($json['text_1']))
{
$textblock =$json['text_1'][0];
echo $textblock['text'];
}
这是给予
名字叫卡索
但是我想
姓名\n卡索
怎么办?好吧,出乎意料的是,它会给你,
Name\nKarSho
作为输出,但是当你用HTML(在任何浏览器中)呈现它时,你不会看到新行,因为浏览器会忽略多个空格和新行,如果你去查看浏览器的源代码,你会在那里看到新行
如果希望HTML显示新行,请使用
echo nl2br($textblock['text']);
这样,新行将转换为
标记,您将在HTML输出中看到这一点
编辑: 如果您还想在输出(原样)中查看
\n
,只需
echo json_encode($textblock['text']);
要删除引号
echo trim(json_encode($textblock['text']), '"');
使用header函数显示下一行
OP
不需要新行,他还想打印\n
。
您可以转义\n
,例如:
$name = "John \n Will";
echo str_replace("\n", "\\n", $name);
将输出:
John \n Will
您需要像KarShoI在我的答案的第一行中指定的那样显示,OP已经得到了他想要的。@Muhammet您的逻辑是正确的,但对我来说已经足够了。
echo json_encode(“test\n test”)代码>将打印test\/n test
您能告诉我您看到了什么吗?
John \n Will