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 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 MATLAB中streamslice问题
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序