我是php的新手,我需要读取文本文件的第5行,看看它是否包含特定的字符串最有效的方法是什么?我知道我可以用这种方法准备所有的行,我应该在这里放一个计数器,如果是5,就打破循环吗?或者有更好的方法吗。。
$file = fopen($fileName,'r');
while(!feof($file))
{
$name = fgets($file);
}
fclose($file);
编辑
我已经更新了我的代码,但strpos总是找不到字符串,即使它存在。。我已经验证了第5行是这样的包装名称:com.boom.appfree
//read entire file into an array
$lines = file($target_path);
//check line 5 if it contains free
$pos = strpos($line[4], 'free');
if ($pos === false)
{
$subject = "Paid Log uploaded";
} else
{
$subject = "Log uploaded";
}
有什么建议吗。。
$lines =
file
("filename.txt");
然后你可以得到第五行:$lines[4]
,并用strpos
搜索你要找的单词。
如果文件的行大小相等,则可以使用fseek:http://php.net/manual/en/function.fseek.php跳到第五行。
否则,添加一个计数器并中断while循环就可以了。