ds2128629 2015-06-26 05:36
浏览 32
已采纳

json decode删除新行

Find my Json code.

$result  =  { 
        "text_1": 
            [
                { 
                    "text": "Name 
KarSho", 
                    "left": 0, 
                    "right": 0
                }
            ]
    }

I want "text" from this Json in PHP. I used following script,

$json = json_decode($result,true);
if($json && isset($json['text_1']))
{
    $textblock =$json['text_1'][0];
    echo $textblock['text'];
}

It's giving,

Name KarSho

But I want,

Name KarSho

What to do?

  • 写回答

3条回答 默认 最新

  • dsgw8802 2015-06-26 05:44
    关注

    Well, to your surpeize, it will give you, Name KarSho as output, But when you render that in HTML (In any Browser), You will not see a new line, because more than one spaces and new lines are ignored by browsers, if you go to view source of the browser, you will see a new line there,

    If you want your HTML to show the new line, use

    echo nl2br($textblock['text']);
    

    So that your new lines will be converted to <br> tag, and you will see that in your HTML output.


    Edit:

    If you want to see also in output (as is), You just want

    echo json_encode($textblock['text']);
    

    and to remove the quotes,

    echo trim(json_encode($textblock['text']), '"');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 请问有会的吗,用MATLAB做
  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 ARIMA模型时间序列预测用pathon解决
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序,怎么查看客户esp32板子上程序及烧录地址
  • ¥50 html2canvas超出滚动条不显示