我正在使用以下脚本,以便从文本文件中获取特定的行:
$searchfor = "Address to be geocoded";
// get the file contents
$contents = file_get_contents('predictVdslEligibilityWSLog-20150614.txt');
// escape special characters in the query
$pattern = preg_quote($searchfor, '/');
// finalise the regular expression, matching the whole line
$pattern = "/^.*$pattern.*'$/m";
// search, and store all matching occurences in $matches
if(preg_match_all($pattern, $contents, $matches)){
echo "Found matches:'n";
echo implode("'n", $matches[0]);
}
else{
echo "No matches found";
}
这工作得很好,但我现在想做的是也包括在我的matches()数组也每个匹配的下一行。例如,如果我的文本文件看起来像这样:
Address to be geocoded:....
Other line 1
Other line 2
Address to be geocoded: ...
Other line x
Other line x2
我想得到行"地址是地理编码:...."加行:其他行1和其他行x.
是否可以使用正则表达式和preg_match?
你可以尝试这样做:$pattern = "/^.*$pattern.*'n.*'n.*'$/m";
它将匹配->搜索的元素+行尾+下一行