PHP 使用 str_replace 替换或删除空行的简单方法 [英] PHP Simple way to replace or remove empty lines with str_replace

查看:27
本文介绍了PHP 使用 str_replace 替换或删除空行的简单方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$line-out = str_replace('\r', '', str_replace('\n', '', $line-in));

以上对我有用,但是,我在某处看到了 [\n\r] 示例,但似乎找不到.

The above works for me but, I saw a [\n\r] example somewhere and I cannot seem to find it.

我只想去掉任何空行.以上是在foreach循环中.

I just want to get rid any blank lines. The above is in a foreach loop.

感谢您的教导.

推荐答案

你不应该在变量名中使用 - ;)

You shouldn't use - in variable names ;)

$line_out = preg_replace('/[\n\r]+/', '', $line_in);
$line_out = str_replace(array("\n", "\r"), '', $line_in);

手动输入:

这篇关于PHP 使用 str_replace 替换或删除空行的简单方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆