Problem when retrieving text in JSON format containing line breaks with jQuery

前端 未结 9 2119
独厮守ぢ
独厮守ぢ 2020-12-02 18:05

I\'m having a strange problem when retrieving JSON formatted text. I use jQuery post to send some data (also JSON formatted) to the server (running PHP) which w

相关标签:
9条回答
  • 2020-12-02 19:00

    Like Terw but with replacing \n

    <?php
     $json = str_replace('\n', '', $json);
    ?>
    

    Should remove all line breaks, jquery shouldn't fail over
    tags, but line breaks should not be in JSON.

    0 讨论(0)
  • 2020-12-02 19:01

    do you get linebreaks like <br /> or newlines like \n? But try to replace them with PHP.

    <?php
    $string = 'asdfasf<br />asdfasf';
    echo str_replace('<br />', '', $strin); // Replace <br /> with '' (nothing)
    ?>
    

    or check out urlencode

    0 讨论(0)
  • 2020-12-02 19:08

    Easy, just try:

    <?php
    ...
    function parseline($string){
      $string = str_replace(chr(10), "//n", $string);
      $string = str_replace(chr(13), "//n", $string);
      return $string;
    }
    
    echo parseline($string_with_line_breaks);//returns json readable text
    

    I have tested it and it works perfect. No need to add complicated functions.

    0 讨论(0)
提交回复
热议问题