php中无法替换\r\n和\n的解决方法

原文链接:http://www.cnblogs.com/zhujiecode/archive/2012/03/30/2425736.html

$text="aaaa
bbb
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);

这样即可替换换行符!

你可能感兴趣的:(php中无法替换\r\n和\n的解决方法)