Home  >  Article  >  Backend Development  >  How to solve the problem that the n newline character in the php string is invalid and cannot be broken?

How to solve the problem that the n newline character in the php string is invalid and cannot be broken?

WBOY
WBOYOriginal
2016-07-25 09:12:561353browse

There is the following php code:

  1. echo 'hellon';
  2. echo 'world';
  3. ?>
Copy the code

The newline character n in the program will be output directly, and the newline cannot be changed correctly. Solution Method: Change single quotes to double quotes:

  1. echo "hellon";
  2. echo "world";
  3. ?>
Copy code

In fact, it is the difference between double quotes and single quotes in php, a simple summary Variables in double quotes can be parsed, and single quotes are absolute strings. Example, three ways to remove line breaks in PHP

  1. //php Line breaks in different systems

  2. //The implementation of line breaks in different systems is different
  3. ///n is used in linux and unix
  4. // mac uses /r
  5. //window to reflect the difference from linux, it is /r/n
  6. //so the implementation methods are different on different platforms
  7. //php There are three methods to solve

  8. $str = str_replace(array("/r/n", "/r", "/n"), "", $str);

  9. < ;p>//2. Use regular replacement
  10. $str = preg_replace('//s*/', '', $str);

  11. //3. Use PHP to define it Variables (recommended)

  12. $str = str_replace(php_eol, '', $str);
  13. ?>

Copy code


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn