php - PHP 逐行读取时可以排除换行符吗?

标签 php

我想逐行阅读,但我不想处理换行符,我希望将其删除,这样我最终只能得到该行的内容。

所以现在我的功能是:

function getProductCount($path)
{
   $count = 0;

foreach (file($path) as $name) {
    if($name == "<product>\n")
    {
       $count = $count + 1;
    }
}
       return $count;
}

但理想情况下我想做的是:

function getProductCount($path)
{
   $count = 0;

foreach (file($path) as $name) {
    if($name == "<product>")
    {
       $count = $count + 1;
    }
}
       return $count;
}

有没有办法也删除回车符?

谢谢

最佳答案

http://www.php.net/manual/en/function.file.php

查看可以添加到函数调用中的其他标志。您应该使用“FILE_IGNORE_NEW_LINES”

关于php - PHP 逐行读取时可以排除换行符吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6915801/

相关文章:

php - 与 php 的双值比较问题

php - MySQL 返回空结果集

php - 查找 <a> 和 </a> 标记内的文本

php - 防止自动谷歌缩短 URL 访问

php - Lumen:如何使用 Eloquent 获取子表列表?

php - ZF2 中的生产、开发、暂存配置?

php - 这是什么意思?我该如何解决?未捕获的异常 'PDOException',消息为“SQLSTATE[42000]

php - 使用变量和文本在 PHP 中创建 SQL 表

javascript - 在 jQuery 中通过 AJAX 发送表单,然后保存在 MySQL 中,但不起作用

php - 在单个查询中使用 mysql_insert_id