我有一个preg_match_all,它从一个字符串生成一个url数组。该数组类似于:
$url[0] = "http://www.siteone.com";
$url[1] = "http://www.sitetwo.com";
$url[2] = "http://www.sitethree.com/example1";
$url[3] = "http://www.sitefour.com";
$url[4] = "http://www.sitethree.com/example2";
$url[5] = "http://www.sitefive";
$url[6] = "http://www.sitesix";
$url[7] = "http://www.siteseven";
$url[8] = "http://www.sitethree.com/example3";
但是,我需要能够搜索#url数组,以便在包含“http://www.sitethree.com”时更改该值,并将数组中的此特定值设置为“无值”。因此,一旦应用了这个过程,它就会像这样:
$url[0] = "http://www.siteone.com";
$url[1] = "http://www.sitetwo.com";
$url[2] = "no value";
$url[3] = "http://www.sitefour.com";
$url[4] = "no value";
$url[5] = "http://www.sitefive";
$url[6] = "http://www.sitesix";
$url[7] = "http://www.siteseven";
$url[8] = "no value";
我在循环中尝试过preg_match_all和if语句的多种变体,但却无法得到它。任何帮助将不胜感激。
答案 0 :(得分:2)
$url = array_map(function($v) {
return strpos($v, 'http://www.sitethree.com') === false ? $v : 'no value';
}, $url);
答案 1 :(得分:1)
foreach ($url as &$value) {
if (strpos($value, 'http://www.sitethree.com') === 0) {
$value = 'no value';
}
}
答案 2 :(得分:0)
foreach($url as $id => $link){
if(strstr($link, 'sitethree.com')){
$url[$id] = 'no value';
}
}
print_r($url);
答案 3 :(得分:0)
这样做的一个简单方法是:
foreach($url as $key => $value)
{
if(stristr($value, "http://www.sitethree.com"))
{
$url[$key] = "no value";
}
}