This code is based on a shortcut all the links in the variable $datatext
I want to exclude specific domains from shortcut like google.com|msn.com
code :
<?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;
?>
Use the parse_url function to extract the domain from the URL, but you will need to also to an ends with a string check to see if it's the domain you. Since URLs could be for www.google.com
or drive.google.com
and such.
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;