Home > Backend Development > PHP Tutorial > Example code analysis of how to replace newline characters in php

Example code analysis of how to replace newline characters in php

黄舟
Release: 2023-03-14 14:14:01
Original
1317 people have browsed it

Example code analysis of how to replace line breaks in php

//php 有三种方法来解决 
  
//1、使用str_replace 来替换换行 
$str = str_replace(array("\r\n", "\r", "\n"), "", $str); 
  
//2、使用正则替换 
$str = preg_replace('//s*/', '', $str); 
  
//3、使用php定义好的变量 (建议使用) 
$str = str_replace(PHP_EOL, '', $str);
Copy after login
// 转为前台可显示的换行, nl2br 的方向函数参考php手册
$str = "a
b
e
f
c";

echo nl2br($str);
Copy after login

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

Related labels:
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