-1

我有一个 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 语句的变体,但就是无法得到它。任何帮助将不胜感激。

4

4 回答 4

2
$url = array_map(function($v) {
    return strpos($v, 'http://www.sitethree.com') === false ? $v : 'no value';
}, $url);
于 2012-06-11T13:16:23.027 回答
1
foreach ($url as &$value) {
  if (strpos($value, 'http://www.sitethree.com') === 0) {
    $value = 'no value';
  }
}
于 2012-06-11T13:17:37.047 回答
0
foreach($url as $id => $link){
    if(strstr($link, 'sitethree.com')){
         $url[$id] = 'no value';
    }
}
print_r($url);
于 2012-06-11T13:16:28.447 回答
0

一个简单的方法是这样的:

foreach($url as $key => $value)
{
  if(stristr($value, "http://www.sitethree.com"))
  {
    $url[$key] = "no value";
  }
}
于 2012-06-11T13:16:57.373 回答