【www.gdgbn.com--C语言】
获取字符串中顶级域名代码
<script type="text/网页特效">
var s = b.value.split("n");
var a = [];
var re = new regexp("http://.*?([^.]+.(com.cn|org.cn|net.cn|[^.]+))/.+", "i");
for(var i = 0; i < s.length; i ++){
if(re.test(s[i])){
a.push(s[i].match(re)[1]);
}
else{
a.push("并非合法域名");
}
}
alert(a.join("n"));
</script>
php教程做法
$arr = array(
"http://xxx.com/123.html",
"http://www.xxx.com/123.html",
"http://www.xxx.com.cn/123.html",
"http://www.xxx.org/123.html",
"http://a.www.xxx.net/123.html",
"http://a.b.www.xxx.com/123.html",
"http://a.b.c.www.xxx.net.cn/123.html"
);
$re = "/http://.*?([^.]+.(com.cn|org.cn|net.cn|[^.]+))/.+/";
$ra = array();
for($i = 0; $i < count($arr); $i ++){
if(preg_match($re, $arr[$i])){
preg_match_all($re, $arr[$i], $res, preg_pattern_order);
$ra[] = $res[1][0];
}
else{
$ra[] = "并非合法域名";
}
}
print_r(join("rn", $ra));
?>