배열 내부에서 찾은 값 앞뒤에 10 개의 단어를 인쇄하는 기본 일치 스크립트를 만들려고합니다. 텍스트를 배열로 분할하고 값의 위치를 식별 한 다음 중간에 검색된 값을 사용하여 -10과 +10을 인쇄하여이 작업을 수행했습니다. 그러나 이것은 첫 번째 발생 일뿐입니다. array_keys (52, 78, 80 위치에 있음)를 사용하여 다른 것을 찾을 수 있다는 것을 알고 있지만 array_keys 도 배열을 생성하기 때문에 일치 항목을 순환하는 방법을 잘 모르겠습니다 . 따라서 배열에서 동일한 피연산자를 정수로 사용할 수 없기 때문에 아래 $ location 대신 $ matches ( array_keys 사용 )를 사용하면 작동하지 않습니다. 어떤 제안? 감사합니다!!
<?php
$text = <<<EOD
The spread of a deadly new virus is accelerating, Chinese President Xi Jinping warned, after holding a special government meeting on the Lunar New Year public holiday.
The country is facing a "grave situation" Mr Xi told senior officials.
The coronavirus has killed at least 42 people and infected some 1,400 since its discovery in the city of Wuhan.
Meanwhile, UK-based researchers have warned of a real possibility that China will not be able to contain the virus.
Travel restrictions have come in place in several affected cities. From Sunday, private vehicles will be banned from central districts of Wuhan, the source of the outbreak.
EOD;
$new = explode(" ", $text);
$location = array_search("in", $new, FALSE);
$concordance = 10;
$top_range = $location + $concordance;
$bottom_range = $location - $concordance;
while($bottom_range <= $top_range) {
echo $new[$bottom_range] . " ";
$bottom_range++;
}
?>
를 array_keys
사용 하여 위치의 양쪽 단어 array_slice
를 추출 하고 문장을 다시 합치는 데 사용하여 에서 반환 된 값을 반복 할 수 있습니다 .$concordance
implode
$words = explode(' ', $text);
$concordance = 10;
$results = array();
foreach (array_keys($words, 'in') as $idx) {
$results[] = implode(' ', array_slice($words, max($idx - $concordance, 0), $concordance * 2 + 1));
}
print_r($results);
산출:
Array
(
[0] => least 42 people and infected some 1,400 since its discovery in the city of Wuhan.
Meanwhile, UK-based researchers have warned of a
[1] => not be able to contain the virus.
Travel restrictions have come in place in several affected cities. From Sunday, private vehicles will
[2] => able to contain the virus.
Travel restrictions have come in place in several affected cities. From Sunday, private vehicles will be banned
)
단어 내에서 단어가 두 번 나오는 유사한 구문을 생성하지 않으려면 $concordance
(예 : 위 배열의 인덱스 1과 2) 마지막 일치의 끝 위치를 유지하고 해당 일치에서 발생하는 항목을 건너 뛸 수 있습니다.
$words = explode(' ', $text);
$concordance = 10;
$results = array();
$last = 0;
foreach (array_keys($words, 'in') as $idx) {
if ($idx < $last) continue;
$results[] = implode(' ', array_slice($words, max($idx - $concordance, 0), $concordance * 2 + 1));
$last = $idx + $concordance;
}
print_r($results);
산출
Array
(
[0] => least 42 people and infected some 1,400 since its discovery in the city of Wuhan.
Meanwhile, UK-based researchers have warned of a
[1] => not be able to contain the virus.
Travel restrictions have come in place in several affected cities. From Sunday, private vehicles will
)
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다