2013-11-04 71 views
12

我有一個應用程序需要打開文件,然後找到它的字符串,並打印一個行號找到字符串。PHP - 查找文件中的字符串,然後顯示它的行號

例如,文件example.txt中包含幾個哈希:

APLF2J51 1a79a4d60de6718e8e5b326e338ae533
EEQJE2YX 66b375b08fc869632935c9e6a9c7f8da O87IGF8R
c458fb5edb84c54f4dc42804622aa0c5 APLF2J51
B7TSW1ZE 1e9eea56686511e9052e6578b56ae018
EEQJE2YX affb23b07576b88d1e9fea50719fb3b7


所以,我希望PHP搜索「1e9eea56686511e9052e6578b56ae018」,並打印出它的行號,在這種情況下,4

請注意,有不會在文件中的多個哈希值。

我在互聯網上發現了一些代碼,但似乎沒有工作。

我這個嘗試之一:

<?PHP 
$string = "1e9eea56686511e9052e6578b56ae018"; 
$data = file_get_contents("example.txt"); 
$data = explode("\n", $data); 
for ($line = 0; $line < count($data); $line++) { 
if (strpos($data[$line], $string) >= 0) { 
die("String $string found at line number: $line"); 
} 
} 
?> 

它只是說該字符串在行0 ....這是不正確的發現....

最終應用比這複雜得多... 它開創行號後,就應更換串別的東西,並更改保存到文件中,然後繼續進一步處理....

感謝提前:)

+2

而且你可以嘗試? –

+1

我剛剛添加了我的最後一次嘗試。謝謝你提醒我。 – xZero

+3

你自己的嘗試不起作用的原因是,當找不到針時,strpos會返回錯誤,這會使你的if語句評估爲真。您必須編寫'!== false'來使這樣的語句有效。 –

回答

12

的超基性的解決辦法是:

$search  = "1e9eea56686511e9052e6578b56ae018"; 
$lines  = file('example.txt'); 
$line_number = false; 

while (list($key, $line) = each($lines) and !$line_number) { 
    $line_number = (strpos($line, $search) !== FALSE) ? $key + 1 : $line_number; 
} 

echo $line_number; 

內存金丹版本,更大的文件:

$search  = "1e9eea56686511e9052e6578b56ae018"; 
$line_number = false; 

if ($handle = fopen("example.txt", "r")) { 
    $count = 0; 
    while (($line = fgets($handle, 4096)) !== FALSE and !$line_number) { 
     $count++; 
     $line_number = (strpos($line, $search) !== FALSE) ? $count : $line_number; 
    } 
    fclose($handle); 
} 

echo $line_number; 
+0

它不起作用。它不會輸出任何內容... 是的,我試過echo $ line_number; 無論如何,謝謝。 – xZero

+0

現在它像魅力一樣工作。選擇最好的答案是因爲我喜歡使用file()而不是file_get_contents();編輯:內存節省版本不起作用。輸出爲空白。 – xZero

+0

現在我嘗試了內存保護程序版本,並且它也可以很好地工作。謝謝! – xZero

2
function get_line_from_hashes($file, $find){ 
    $file_content = file_get_contents($file); 
    $lines = explode("\n", $file_content); 

    foreach($lines as $num => $line){ 
     $pos = strpos($line, $find); 
     if($pos !== false) 
      return $num + 1 
    } 
    return false 
} 

get_line_from_hashes("arquivo.txt", "[email protected]"); //return some number or false case not found. 
+1

你忘了添加;在return-s上,所以它會拋出錯誤。 返回$ num + 1; 返回false; 糾正後效果很好。謝謝! – xZero

+1

警告:如果文件很大,如果使用'file_get_contents()'和'explode()',內存消耗會很高。 – Raptor

+0

是的,我知道。但現在它將用於最大500kb的文件。 我不知道如何降低內存消耗.... – xZero

2

如果你需要也正在尋找行號快速和通用的解決方案文件中多行文本的使用:

$file_content = file_get_contents('example.txt'); 

$content_before_string = strstr($file_content, $string, true); 

if (false !== $content_before_string) { 
    $line = count(explode(PHP_EOL, $content_before_string)); 
    die("String $string found at line number: $line"); 
} 

FYI只適用於PHP 5.3.0+。

+1

也適用於7.1,非常快速和簡單。 – INA2N

1
$pattern = '/1e9eea56686511e9052e6578b56ae018/'; 
if (preg_match($pattern, $content, $matches, PREG_OFFSET_CAPTURE)) { 
    //PREG_OFFSET_CAPTURE will add offset of the found string to the array of matches 
    //now get a substring of the offset length and explode it by \n 
    $lineNumber = count(explode("\n", substr($content, 0, $matches[0][1]))); 
} 
相關問題