Gets the top-level domain name code in the string

Source: Internet
Author: User
Tags php tutorial

Gets the top-level domain name code in the string

<body>
<textarea id= "B" >
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</textarea>
<script type= "text/web Effects" >
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 ("Not Legal domain name");
}
}
Alert (A.join ("n"));
</script>
</body>

PHP Tutorial Practices

<?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 [] = "is not a legal domain name";
}
}
Print_r (Join ("RN", $ra));
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.