我从MySQL获取的地址如下:
$address="This Street, Nice Area, That Country";
$address="This Street, Superb Area, Which Country";
$address="This Street, Fine Area, A Country";
现在我要做的是找到AREA - 就像$area=array("Nice", "Good")
。因此,如果在$area
中找到$address
中定义的值,则应列出或留空。
答案 0 :(得分:0)
尝试类似
的内容$area=array(1=>"Nice", "Good");
$address = 'This Street, Nice Area, That Country';
preg_match(', (\w+) Area,', $address, $res);
$is_area = array_search( $res[1], $area, true) ;
if($is_area) echo $address;
<强>解释强>
第一行设置区域数组以索引1而不是零开始(确保查找不返回零)
preg_match行从地址
中提取区域数组搜索行查找区域数组中的区域
答案 1 :(得分:0)
我认为你所要求的内容可以解释为:
“列出包含$area
数组中找到的字符串实例的所有$ address字符串。”
在哪种情况下:
// Set up.
$address = array(
"This Street, Nice Area, That Country",
"This Street, Superb Area, Which Country",
"This Street, Fine Area, A Country",
);
$area = array("Nice", "Good");
// The search.
$results = array();
foreach ($address as $haystack) {
foreach ($area as $needle) {
$regexPattern = "#{$needle}#";
if (preg_match($regexPattern, $haystack))
array_push($results, $haystack);
}
}
// The results.
foreach ($results as $result) {
echo "<p>{$result}</p>";
}