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

在php中查找文本文件中以字符开头然后是空格的所有行,php,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;} 使用以下命令: $file = file_get_contents('cached-con

我有一个文本文件,其中我只想读取以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;}
使用以下命令:

$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
)。

使用以下方法:

$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
)。

您所说的“似乎不起作用”是什么意思?这无助于任何人回答你。你怎么知道它是否有效?
$pos
等于什么
0
表示它是字符串的开头。如果(strpos($line,$r)==0)使用特定于类型的值检查,则post示例数据也
;否则0==False“似乎不起作用”是什么意思?这无助于任何人回答你。你怎么知道它是否有效?
$pos
等于什么
0
表示它是字符串的开头。如果(strpos($line,$r)==0)使用特定于类型的值检查,则post示例数据也
;否则0==false