Home  >  Article  >  php教程  >  PHP中替换换行符方法总结

PHP中替换换行符方法总结

WBOY
WBOYOriginal
2016-06-13 08:50:39935browse

PHP中替换换行符方法总结

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);

转为前台可显示的换行, nl2br 的方向函数参考php手册

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

输出html代码如下:

a

b

e

f

c

您可能感兴趣的文章

  • php利用正则过滤各种标签,空格,换行符的代码
  • php如何将html中的br换行符转换为文本输入中的换行符
  • PHP压缩html网页代码(清除空格,换行符,制表符,注释标记)
  • css控制英文、中文强制换行与不换行的代码
  • php字符串替换函数str_replace速度比preg_replace快
  • PHP 只替换一次,控制替换次数
  • PHP压缩html网页代码减小网络数据传输量,清除空格,制表符,注释标记
  • 在php中分别使用curl的post提交数据的方法和get获取网页数据的方法总结
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