1

此代码基于变量中的所有链接的快捷方式$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;
 ?>
4

1 回答 1

0

使用parse_url函数从 URL 中提取域,但您还需要以字符串结尾来检查它是否是您的域。因为 URL 可能是 for www.google.comordrive.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;
于 2013-05-24T23:13:43.490 回答