1

我有从 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,则应列出或留空。

4

2 回答 2

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>";
}
于 2013-06-18T13:04:13.220 回答
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 行从地址中提取区域

数组搜索线查找您的区域数组中的区域

于 2013-05-25T14:40:42.527 回答