PHP可以在逐行阅读时排除换行符吗?

jma*_*erx 2 php

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

所以现在我的功能是:

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

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

但理想情况下我想做:

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

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

有没有办法也可以删除回车?

谢谢

TRD*_*TRD 11

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

查看可以添加到函数调用的其他标志.你应该使用"FILE_IGNORE_NEW_LINES"