我有一个文本文件,在其中我只想读取以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;}
答案 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
)。