从URL 中取出域名
2016-05-08 17:57
218 查看
实例 3-29 从URL 中取出域名
本实例演示了如何从URL 中取出域名,如代码3-29 所示。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> preg_match_3.php </title>
<meta charset="UTF-8">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
</head>
<body>
<?php
// 从 URL 中取得主机名
preg_match("/^(http:\/\/)?([^\/]+)/i",
"http://www.php.net/index.html", $matches);
//获取主机名
$host = $matches[2];
// 从主机名中取得后面两段得到域名
preg_match("/[^\.\/]+\.[^\.\/]+$/", $host, $matches);
echo "域名为: {$matches[0]}\n";
?>
</body>
</html>
域名为: php.net
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> preg_match_3.php </title>
<meta charset="UTF-8">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
</head>
<body>
<?php
// 从 URL 中取得主机名
preg_match("/^(http:\/\/)?([^\/]+)/i",
"http://www.php.net/index.html", $matches);
var_dump($matches);
//获取主机名
$host = $matches[2];
var_dump($host);
// 从主机名中取得后面两段得到域名
preg_match("/[^\.\/]+\.[^\.\/]+$/", $host, $matches);
var_dump($matches);
echo "域名为: {$matches[0]}\n";
?>
</body>
</html>
array
0 => string 'http://www.php.net' (length=18)
1 => string 'http://' (length=7)
2 => string 'www.php.net' (length=11)
string 'www.php.net' (length=11)
array
0 => string 'php.net' (length=7)
域名为: php.net
本实例演示了如何从URL 中取出域名,如代码3-29 所示。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> preg_match_3.php </title>
<meta charset="UTF-8">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
</head>
<body>
<?php
// 从 URL 中取得主机名
preg_match("/^(http:\/\/)?([^\/]+)/i",
"http://www.php.net/index.html", $matches);
//获取主机名
$host = $matches[2];
// 从主机名中取得后面两段得到域名
preg_match("/[^\.\/]+\.[^\.\/]+$/", $host, $matches);
echo "域名为: {$matches[0]}\n";
?>
</body>
</html>
域名为: php.net
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> preg_match_3.php </title>
<meta charset="UTF-8">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
</head>
<body>
<?php
// 从 URL 中取得主机名
preg_match("/^(http:\/\/)?([^\/]+)/i",
"http://www.php.net/index.html", $matches);
var_dump($matches);
//获取主机名
$host = $matches[2];
var_dump($host);
// 从主机名中取得后面两段得到域名
preg_match("/[^\.\/]+\.[^\.\/]+$/", $host, $matches);
var_dump($matches);
echo "域名为: {$matches[0]}\n";
?>
</body>
</html>
array
0 => string 'http://www.php.net' (length=18)
1 => string 'http://' (length=7)
2 => string 'www.php.net' (length=11)
string 'www.php.net' (length=11)
array
0 => string 'php.net' (length=7)
域名为: php.net
相关文章推荐
- 6、Activity生命周期、Activity间数据传递
- ListView使用技巧
- stnemelEtneuqerFKpoT.347
- python+opencv实现阈值分割
- c++实验5——数组分离
- 第十一讲 项目3 买五赠一 买二十赠五 优惠计算
- CSS选择器
- Android之十二微信UI界面设计
- 5、HttpClient、多线程下载、HttpUtils
- Qt和Qml交互,及多线程
- 排序算法——冒泡排序
- OC 类对象
- Java 设计模式(十) 单一职责原则(SRP)
- Java中继承thread类与实现Runnable接口的区别
- Toobar样式
- 20145325张梓靖 《Java程序设计》第10周学习总结
- 【LeetCode】Remove Duplicates from Sorted Array 解题报告
- 4. Http请求以及提交数据、图片缓存
- 怪物农场2修改日志3 - 年轮
- Eclipse中有效解决安卓R文件丢失