Home  >  Article  >  Backend Development  >  How to replace newline characters in PHP

How to replace newline characters in PHP

coldplay.xixi
coldplay.xixiOriginal
2020-08-05 10:30:594290browse

How to replace newline characters in php: First create a new PHP document and define the text; then save it, print it in the browser, and define an array containing the newline characters for all platforms; then use the [str_replace()] function , replace the newline character with [
]; finally save it and view the effect in the browser.

How to replace newline characters in PHP

php method to replace newlines:

1. Before writing a PHP script, you first need to know the On different platforms, the newline characters are different.

  • Widows system newline character: \r\n

  • Linux system newline character :\n

  • Mac system newline character:\r

How to replace newline characters in PHP

##Related learning recommendations :

php graphic tutorial

2. Create a new PHP document and define a paragraph of text, example:

How to replace newline characters in PHP

3. Save the above content, view and print it in the browser. The default line break character will not be displayed in the browser, because the browser understands the line break character in HTML and it should be


How to replace newline characters in PHP

4. Define an array that contains line breaks for all platforms. Example:

$newline = array("\r\n","\n","\r");

Note: Line breaks must use double quotes (" ") includes

How to replace newline characters in PHP##5. Use the str_replace() function to replace the newline character with
, example:

$br = str_replace($newline,'
',$display);

How to replace newline characters in PHP6. Save the above content and view the effect after replacement in the browser

How to replace newline characters in PHP

Related video recommendations:
PHP programming from entry to proficiency

The above is the detailed content of How to replace newline characters in PHP. For more information, please follow other related articles on the PHP Chinese website!

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