如何解决PHP字符串中的换行符无效且多余的换行符

 2667

60655dce375c5.png


换行符无效:

示例:

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

上面出现的问题主要是因为php中单引号和双引号都能表示,但是双引号还有一个特别的功能:双引号串中的内容可以被解释而且替换,这是单引号所没有的。

修改后:

<?php 
    echo "hello\n"; 
    echo 'world'; 
?>

多余换行符:

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


TAG标签:
本文网址:https://www.zztuku.com/detail-8758.html
站长图库 - 如何解决PHP字符串中的换行符无效且多余的换行符
申明:如有侵犯,请 联系我们 删除。

评论(0)条

您还没有登录,请 登录 后发表评论!

提示:请勿发布广告垃圾评论,否则封号处理!!

    编辑推荐