我正在尝试创建一个基本的一致性脚本,该脚本将打印数组内找到的值之前和之后的十个单词。为此,我将文本拆分为一个数组,标识值的位置,然后打印 -10 和 +10,中间显示搜索的值。但是,这只是第一次出现这种情况。我知道我可以通过使用array_keys找到其他人(在位置52,78,80中找到),但我不太确定如何循环完成匹配,因为array_keys也会产生数组。因此,使用$matches(带有array_keys)代替下面的$location不起作用,因为您不能在数组上使用相同的操作数作为整数。有什么建议吗?谢谢!!
<?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++;
}
?>
慕斯王
九州编程