可以使用fgets()
函数来获取行号。
喜欢的东西:
$handle = fopen("forms.php", "r");
$found = false;
if ($handle)
{
$linecount = 0;
while (($buffer = fgets($handle, 4096)) !== false)
{
if (strpos($buffer, "$searchterm") !== false)
{
echo "Found on line " . $countline + 1 . "\n";
$found = true;
}
$countline++;
}
if (!$found)
echo "$searchterm not found\n";
fclose($handle);
}
如果你还是想用file_get_contents()
,然后像做:
$homepage = file_get_contents("forms.php");
$exploded_page = explode("\n", $homepage);
$found = false;
for ($i = 0; $i < sizeof($exploded_page); ++$i)
{
if (strpos($buffer, "$searchterm") !== false)
{
echo "Found on line " . $countline + 1 . "\n";
$found = true;
}
}
if (!$found)
echo "$searchterm not found\n";
魔术串第一次出现短路。也不返回该行,只返回该字符串发生的行号。 – Hasteur 2012-03-15 16:52:26
我能够通过回显$行来获得该行的内容。很棒! – 2012-03-15 16:56:17
欧,对不起,我以为你只想要行号,现在已经修好了。 – martin 2012-03-15 19:47:52