PHP:搜索行范围文件txt中的文本/单词
我想使用php从文本文件中查找行范围内的单词。以下是文本文件:PHP:搜索行范围文件txt中的文本/单词,php,file,search,range,line,Php,File,Search,Range,Line,我想使用php从文本文件中查找行范围内的单词。以下是文本文件: 1 asdcasdc 2 asdcadsc 3 asdc found asdcac 4 ascdsc found asd 5 kjhblk 6 kjn 7 found asdcac 8 asdcasd found 9 asdcasdc 10 asdc 11 asdc 我想在文本文件行的5-11范围内搜索“找到”。如何获得如下输出: 7 found asdcac 8 asdcasd found 任何帮助都将不胜感激 -php no
1 asdcasdc
2 asdcadsc
3 asdc found asdcac
4 ascdsc found asd
5 kjhblk
6 kjn
7 found asdcac
8 asdcasd found
9 asdcasdc
10 asdc
11 asdc
我想在文本文件行的5-11范围内搜索“找到”。如何获得如下输出:
7 found asdcac
8 asdcasd found
任何帮助都将不胜感激
-php noobie像这样的东西就可以了
$content = file(__DIR__ . '/yourfile.txt');
// Grab the lines you want to search between
// This will search lines 5-11
$lines = array_slice($content, 5, 6, true);
$found = array_filter($lines, function($line) { return strpos($line, 'found') !== false; });
print_r($found);
只需将文件内容更改为实际文件即可
此外,对于一些参考,这里的和可能是类似regex的(^(?[5-9]|[1-9][0-9]+).*已找到。*)
。。。