我有一组数组
array('www.example.com/','www.example.com','www.demo.example.com/','www.example.com/blog','www.demo.com');
我希望得到所有符合以下模式的元素,
$matchArray = array('*.example.com/*','www.demo.com');
预期结果为
array('www.example.com/','www.demo.example.com/','www.example.com/blog','www.demo.com');
谢谢:)
答案 0 :(得分:1)
这有效:
$valuesArray = array('www.example.com/','www.example.com','www.demo.example.com/','www.example.com/blog','www.demo.com');
$matchArray = array('*.example.com/*','www.demo.com');
$matchesArray = array();
foreach ($valuesArray as $value) {
foreach ($matchArray as $match) {
//These fix the pseudo regex match array values
$match = preg_quote($match);
$match = str_replace('\*', '.*', $match);
$match = str_replace('/', '\/', $match);
//Match and add to $matchesArray if not already found
if (preg_match('/'.$match.'/', $value)) {
if (!in_array($value, $matchesArray)) {
$matchesArray[] = $value;
}
}
}
}
print_r($matchesArray);
但我建议将匹配数组的语法更改为实际的正则表达式模式,以便不需要代码的修复部分。
答案 1 :(得分:0)
/\w+(\.demo)?\.example\.com\/\w*|www\.demo\.com/