查找文本文件中以字符开头的所有行,然后查找php中的空格

时间:2013-06-25 20:24:39

标签: php

我有一个文本文件,在其中我只想读取以r开头然后是空格的行。 我试过strpos,但似乎没有用。

$r="r ";  
$file = file_get_contents('cached-consensus', true);  
$n = explode("\n", $file);  
foreach($n as $line){  
$pos= strpos($line,$r);  
echo $pos;}

1 个答案:

答案 0 :(得分:7)

使用此:

$file = file_get_contents('cached-consensus', true);  
$n = explode("\n", $file);  
foreach($n as $line){  
    if(0 === strpos($line, "r ")){
        // do whatever you want with the line
    }
}

这会检查$line是否以' r开头。目前,您只是回显字符串的位置(这应该是0)。