search
Homephp教程php手册PHP中替换换行符的几种方法小结

PHP中替换换行符的几种方法小结

Jun 13, 2016 am 11:56 AM
phpstrtestthiscodeseveral kindscopynewline charactermethodreplaceof

第一种:

复制代码 代码如下:


?$str="this is a test \n";
$patten = array("\r\n", "\n", "\r");
?//先替换掉\r\n,然后是否存在\n,最后替换\r
$str=str_replace($order, "", $str);
?>


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

复制代码 代码如下:


/*
* 获得用户操作系统的换行符,\n
* @access public
* @return string
*/
function get_crlf()
{
if (stristr($_SERVER['HTTP_USER_AGENT'], 'Win'))
{
$the_crlf = '\r\n';
}
elseif (stristr($_SERVER['HTTP_USER_AGENT'], 'Mac'))
{
$the_crlf = '\r'; // for old MAC OS
}
else
{
$the_crlf = '\n';//权重大一点
}
return $the_crlf;
}


注意:在前台页面显示的时候,用nl2br使换行变成


第二种实例说明:

发现一个有趣的事情:

$text="aaaa


ccc";

$text=str_replace('\n‘,"",$text);
$text=str_replace('\r‘,"",$text);
$text=str_replace('\r\n‘,"",$text);

正常来说,上面的代码应该可以替换换行符了吧

但是事实上却是不可以!

很郁闷,试了很多次,就是不起作用。

最后改成这样

复制代码 代码如下:


$text=str_replace("\n","",$text);
$text=str_replace("\r","",$text);
$text=str_replace("\r\n","",$text);


居然一切OK了~~,原来是双引号,单引号的问题!!

双引号 比单引号效率差点,因为双引号在被php解析的过程中 ,还会判断里面会不会有变量,单引号就不会有这个判断,故而一般来讲,没涉及到变量的情况下,我都会用单引号,没想到这次替换换行符,用单引号居然不行·····

最后写成一句话

复制代码 代码如下:


$order = array("\r\n", "\n", "\r");
$replace = '';
$text=str_replace($order, $replace, $text);


这样即可替换换行符!
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!