how to remove new lines and returns from php string?

后端 未结 10 1106
孤城傲影
孤城傲影 2020-12-24 01:06

A php variable contains the following string:

text

text2

  • item1
  • item2
10条回答
  •  北海茫月
    2020-12-24 01:51

    You need to place the \n in double quotes.
    Inside single quotes it is treated as 2 characters '\' followed by 'n'

    You need:

    $str = str_replace("\n", '', $str);
    

    A better alternative is to use PHP_EOL as:

    $str = str_replace(PHP_EOL, '', $str);
    

提交回复
热议问题