我想在此代码中排除特定域

时间:2013-05-24 22:57:09

标签: php api

此代码基于变量$datatext中所有链接的快捷方式 我想从google.com|msn.com

等快捷方式中排除特定域名

代码:

<?php
$datatext = "bla bla bla bla <br /><a href=\"http://www.google.com\" target=\"_blank\">http://www.google.com</a><br /><br /><a href=\"http://www.msn.com\" target=\"_blank\">http://www.msn.com</a><br /><br /><a href=\"http://www.edaf.at\" target=\"_blank\">http://www.edaf.at</a> bla bla bla bla bla bla bla bla";
$wreplacer_first = 'target="_blank">www.';
$wreplacer_second = 'target="_blank">http://www.';
$data = str_replace("$wreplacer_first","$wreplacer_second",$datatext);
$reg_exUrl = "#\<a href=\"(.*)\" target=\"(.*)\"\>(.*)<\/a\>#Ui";

    preg_match_all($reg_exUrl, $data, $matches);
    $links = $matches[1];

    foreach ($links as $val)
    { 
    $turl=$val;
      $link="http://v.gd/create.php?format=simple&url=".$turl."";
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($ch, CURLOPT_TIMEOUT, 1);
      curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1);
      curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
      curl_setopt($ch, CURLOPT_URL, $link);
      $content = curl_exec($ch);  
      curl_close($ch);
      $datatext = str_replace($turl,"$content", $datatext);
      }
     echo $datatext;
 ?>

1 个答案:

答案 0 :(得分:0)

使用parse_url功能从网址中提取域名,但您还需要通过字符串检查结束,看看它是否属于您的域名。由于网址可能适用于www.google.comdrive.google.com等。

 function endsWith($haystack, $needle)
 {
     $length = strlen($needle);
     if ($length == 0) {
         return true;
     }
     return (substr($haystack, -$length) === $needle);
 }

$blacklist = array("google.com","msn.com");

foreach ($links as $val)
{ 
    $skip = false;
    foreach($blacklist as $domain)
    {
        if(endsWith(parse_url($val,PHP_URL_HOST),$domain))
        {
            $skip = true;
            break;
        }
    }
    if($skip) continue;