PHP - 在文件中找到一个字符串然后显示它的行号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19760564/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP - Find a string in file then show it's line number
提问by xZero
I have an application which needs to open the file, then find string in it, and print a line number where is string found.
我有一个应用程序需要打开文件,然后在其中查找字符串,并在找到字符串的位置打印行号。
For example, file example.txtcontains few hashes:
例如,文件example.txt包含很少的哈希值:
APLF2J51 1a79a4d60de6718e8e5b326e338ae533
EEQJE2YX 66b375b08fc869632935c9e6a9c7f8da O87IGF8R
c458fb5edb84c54f4dc42804622aa0c5 APLF2J51
B7TSW1ZE 1e9eea56686511e9052e6578b56ae018
EEQJE2YX affb23b07576b88d1e9fea50719fb3b7
APLF2J51 1a79a4d60de6718e8e5b326e338ae533
EEQJE2YX 66b375b08fc869632935c9e6a9c7f8da O87IGF8R
c458fb5edb84c54f4dc42804622aa0c5 APLF2J51
B7TSW1ZE 1e9eea56686511e9052e6578b56ae018
EEQJE2YX affb23b07576b88d1e9fea50719fb3b7
So, I want to PHP search for "1e9eea56686511e9052e6578b56ae018" and print out its line number, in this case 4.
所以,我想 PHP 搜索“1e9eea56686511e9052e6578b56ae018”并打印出它的行号,在这种情况下是 4。
Please note that there are will not be multiple hashes in file.
请注意,文件中不会有多个哈希值。
I found a few codes over Internet, but none seem to work.
我在 Internet 上找到了一些代码,但似乎都不起作用。
I tried this one:
我试过这个:
<?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");
}
}
?>
It just says that string is found at line 0.... Which is not correct....
它只是说在第 0 行找到了字符串......这是不正确的......
Final application is much more complex than that... After it founds line number, it should replace string which something else, and save changes to file, then goes further processing....
最终的应用程序比这复杂得多......找到行号后,它应该替换其他字符串,并将更改保存到文件,然后进一步处理......
Thanks in advance :)
提前致谢 :)
回答by Edakos
An ultra-basic solution could be:
一个超基本的解决方案可能是:
$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;
A memory-saver version, for larger files:
内存节省版本,用于较大的文件:
$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;
回答by Guilherme Soares
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", "asdsadas2e3xe3ceQ@E"); //return some number or false case not found.
回答by Alexander Yancharuk
If you need fast and universal solution that working also for finding line number of multiline text in file, use this:
如果您需要快速且通用的解决方案,该解决方案也可用于在文件中查找多行文本的行号,请使用以下命令:
$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");
}
FYIWorks only with PHP 5.3.0+.
仅供参考仅适用于 PHP 5.3.0+。
回答by Eugene Tulika
$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])));
}

