Home > Backend Development > PHP Tutorial > Provide several php methods to replace newline characters

Provide several php methods to replace newline characters

WBOY
Release: 2016-07-25 09:05:50
Original
1079 people have browsed it
  1. ?$str="this is a test n";
  2. $patten = array("rn", "n", "r");
  3. ?//Replace rn first, Then if n exists, finally replace r
  4. $str=str_replace($order, "", $str);
  5. ?>
Copy code
  1. //php has three ways to solve it
  2. //1. Use str_replace to replace newlines
  3. $str = str_replace(array("rn", "r", "n"), "", $str);
  4. //2. Use regular replacement

  5. $str = preg_replace('//s*/', '', $str);

  6. //3. Use php Defined variables (recommended to use)

  7. $str = str_replace(PHP_EOL, '', $str);
Copy code
  1. /*
  2. * Get the newline character of the user's operating system, n
  3. * @access public
  4. * @return string
  5. */
  6. function get_crlf()
  7. {
  8. if (stristr($_SERVER['HTTP_USER_AGENT'], 'Win'))
  9. {
  10. $the_crlf = 'rn';
  11. }
  12. elseif (stristr($_SERVER['HTTP_USER_AGENT'], 'Mac'))
  13. {
  14. $the_crlf = 'r'; // for old MAC OS
  15. }
  16. else
  17. {
  18. $the_crlf = 'n';//The weight is bigger
  19. }
  20. return $the_crlf;
  21. }
Copy the code

Note: When the front page is displayed, use nl2br to change the line break to

The second example:

I found an interesting thing:

  1. $text="aaaa

  2. ccc";

  3. $text=str_replace('n',"",$text);

  4. $text= str_replace('r',"",$text);
  5. $text=str_replace('rn',"",$text);

Copy code

Normally, the above The code should replace newlines, but it doesn't. Very frustrated, tried many times, but it just doesn't work. Finally changed it to this:

  1. $text=str_replace("n","",$text);
  2. $text=str_replace("r","",$text);
  3. $text=str_replace("rn"," ",$text);
Copy the code

and it works. It turns out to be a problem with double quotes and single quotes!

Double quotation marks are less efficient than single quotation marks, because during the process of parsing double quotation marks by PHP, it will also judge whether there are variables inside, but single quotation marks will not make this judgment, so generally speaking, variables are not involved. I always use single quotes, but I didn’t expect that replacing the newline character this time would not work using single quotes...

Finally written as:

  1. $order = array("rn", "n", "r");
  2. $replace = '';
  3. $text=str_replace($order, $replace, $text);
Copy the code

so you can replace the newlines. Isn't it very convenient?

>>>> Articles you may be interested in: php remove string newline character instance analysis php compress html (clear newlines, clear tabs, remove comment marks) How to convert textarea line breaks in php forms Code examples of php regular filtering html tags, spaces, newlines, etc. Summary of how to remove line breaks in php How to compress html webpage code with php (remove spaces, newlines, tabs, comment marks, etc.) php generates excel and controls line breaks in Excel cells



source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template