텍스트 파일에서 특정 줄을 가져 오기 위해 다음 스크립트를 사용하고 있습니다.
$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";
.-> 검색 요소 + 줄의 끝 + 다음 줄 모두와 일치합니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다