duanchou6534 2016-10-10 16:35
浏览 653


I am building an output array like so

            if (count($errors)) {
                $success = 'false'; 
                $output['json_msg'] = "Please try your submission again.";
                $output['errors'] = $errors;
            } else {
                $success = 'true';  
                $output['json_msg'] = "Thanks for Becoming a NOLA Insider!";

            $output['success'] = $success;

            if (count($errors)) { http_response_code(500); }
            echo json_encode($output);          

But when I look at the response in Chrome's Network pane of the developer tools I see what appears to be a newline in response:

developer console screenshot

I tried wrapping json_encode() in trim() but this gave garbled output.

How do I eliminate the carriage return?

  • 写回答

4条回答 默认 最新

  • doukuizuo1795 2016-10-10 17:07

    Do you have a ?> at the end of your PHP file and what's happening when you remove it ?

    Because you may have a carriage return at the end of the script which may be sent before your response :

    // END OF FILE

    This is explained by the fact that PHP is actually a templating language :

    Here is a file which defines a function and which displays a text :

     * @File lib.php
     function sayHello()
         echo "hello";
     forgotten text

    And here is a file that includes this file.

     * @file index.php

    This will output :

    forgotten text

    The "forgotten text" is output when the lib.php file is included whereas the "hello" is output after.

    (But it may be even simpler and just the point that @nanocv suggested)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
  • dongshan3759 2016-10-10 16:47

    You can try to remove new line using str_replace

    $output = str_replace(array("
    ", "
    ", ""),'',$output);
    echo json_encode($output); 
  • douheng8629 2016-10-10 17:04

    If you are getting the new line characters like to your json code after json_encode() you can follow up the method with the final json_value that you get. This will remove up all the new lines that has been output-ed from the code that you obtain after you perform the json_encode().

    Hence you need to preg_replace() the json outputed value as follows which will remove uo the new lines from the json_code.

    This will replace the new lines with no value over to the second parameter in preg_replace().

    Try not to provide any white spaced between the php codes (i.e) Opening and Closing Codes that you process either at the beginning or at the end of the document. This may cause the issue sometimes.


    $output_json = preg_replace("!?
    !","", $output_json);
  • doushenken2833 2016-10-10 17:15

    I bet your php code starts this way:

    1.            <--- Note the blank line here
    2. <?php

    That's a new line character that will became part of the result.

    (This way I could recreate the same behavior)

    enter image description here




  • ¥15 burpsuite密码爆破
  • ¥15 关于#ubuntu#的问题,如何解决?(相关搜索:移动硬盘)
  • ¥15 scikit安装之后import不了
  • ¥15 Ros2编译一个使用opencv的c++节点的时候,报了这个错误,请问怎么解决啊
  • ¥15 人脸识别相关算法 YOLO,AI等
  • ¥15 spark问题方便加V17675797079
  • ¥15 Python代码不打印的原因
  • ¥20 微软SEAL库的安装和使用,VS2022环境配置问题
  • ¥15 数学问题也不知道那种类型的问题
  • ¥15 R作图的时候,文本框重叠了怎么办?