Home > php教程 > php手册 > body text

php 字符串中的n换行符无效、不能换行的解决方法

WBOY
Release: 2016-06-13 09:39:54
Original
820 people have browsed it

例如下面的代码:

复制代码 代码如下:

echo 'hello\n';
echo 'world';
?>


程序的中的换行符\n会直接输出,无法正确换行,解决方法是把单引号改为双引号:

复制代码 代码如下:

echo "hello\n";
echo "world";
?>


这样就可以了!其实就是PHP的双引号和单引号的区别问题,简单个概括下双引号中的变量可以解析,单引号就是绝对的字符串。

附:PHP去除换行的三种方法代码

复制代码 代码如下:

     //php 不同系统的换行 
    //不同系统之间换行的实现是不一样的 
    //linux 与unix中用 /n 
    //MAC 用 /r 
    //window 为了体现与linux不同 则是 /r/n 
    //所以在不同平台上 实现方法就不一样 
    //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);  
?> 

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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!