找出指定域名,排除特定域名后,进行链接的scheme替换
$hostList = array(
"news.emao.com",
"news.m.emao.com",
);
$pattern = "/(http:|(?<!https:))(?:[\/]{2}|[\/\\\]{4})([-a-zA-Z0-9\.]+(?:com(?:|\.cn)|net(?:|\.cn))).*?[\"]/";
$str = preg_replace_callback($pattern, function($matchs) use ($hostList) {
if (! in_array($matchs[2], $hostList)) {
if (preg_match("/emao\.(?:cn|com(?:\.cn|)|net(?:\.cn|))/", $matchs[0])) {
if (empty($matchs[1])) {
$matchs[0] = 'https:'.$matchs[0];
} else {
$matchs[0] = str_replace("http:", "https:", $matchs[0]);
}
} else {
if (empty($matchs[1])) {
$matchs[0] = 'http:'.$matchs[0];
}
}
} else {
if (empty($matchs[1])) {
$matchs[0] = 'http:'.$matchs[0];
}
}
return ($matchs[0]);
}, $str);