Home  >  Article  >  Backend Development  >  Summary of methods for replacing newline characters in PHP_PHP Tutorial

Summary of methods for replacing newline characters in PHP_PHP Tutorial

WBOY
WBOYOriginal
2016-07-12 09:04:35891browse

Summary of methods to replace newline characters in PHP

Summary of methods to replace newline characters in PHP, PHP has three methods to solve it.

1. Use str_replace to replace newlines

$str=str_replace(array("\r\n","\r","\n"),"",$str);

2. Use regular replacement

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

3. Use variables defined by php (recommended)

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

Convert to a line break that can be displayed in the front desk. For the direction function of nl2br, please refer to the PHP manual

$str="a
b
e
f
c";
echo nl2br($str);

The output html code is as follows:

a

b

e

f

c

Articles you may be interested in

  • php code that uses regular rules to filter various tags, spaces, and line breaks
  • How to wrap br in html with php Convert characters into line breaks in text input
  • PHP compresses HTML web page code (clears spaces, newlines, tabs, comment marks)
  • css controls forced line breaks and no line breaks in English and Chinese The code
  • PHP string replacement function str_replace is faster than preg_replace
  • PHP only replaces it once and controls the number of replacements
  • PHP compresses html web page code to reduce the amount of network data transmission. Clear spaces, tabs, and comment marks
  • Summary of how to submit data using curl's post and get to get web page data in PHP

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1072764.htmlTechArticleA summary of the method of replacing line breaks in PHP. A summary of the methods of replacing line breaks in PHP. PHP has three methods to solve it. 1. Use str_replace to replace newlines $str=str_replace(array("rn","r","...
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