您的位置:首页 > 编程语言 > PHP开发

php 使用pathinfo(), parse_url(), basename()解析URL

2016-10-08 12:51 746 查看
本文章向大家介绍解析URL的三种方法,分别为pathinfo()方法、parse_url()方法和basename()方法。每个方法都列举了一个实例,通过实例更容易理解这三个函数的使用方法和技巧,需要的朋友可以参考一下。

1、利用pathinfo解析URL

<?
/* by www.manongjc.com/article/1119.html */
$test = pathinfo("http://localhost/index.php");
print_r($test);
?>


结果如下
Array
(
[dirname] => http://localhost //url的路径
[basename] => index.php //完整文件名
[extension] => php //文件名后缀
[filename] => index //文件名
)


 

2、利用parse_url()函数解析

<?
/* by http://www.manongjc.com     (码农教程)*/
$test = parse_url("http://localhost/index.php?name=tank&sex=1#top");
print_r($test);
?>


结果如下
Array
(
[scheme] => http //使用什么协议
[host] => localhost //主机名
[path] => /index.php //路径
[query] => name=tank&sex=1 // 所传的参数
[fragment] => top //后面根的锚点
)


 

3、使用basename()解析

<?
$test = basename("http://localhost/index.php?name=tank&sex=1#top");
echo $test;
?>


结果如下
index.php?name=tank&sex=1#top

原文地址:http://www.manongjc.com/article/1119.html

其他阅读:

php连接数据库服务器并选择数据库操作实例php使用mysql_result()函数解析结果集数据php mysql_result()函数使用实例php mysql_real_escape_string addslashes及mysql绑定参数防SQL注入攻击php mysql_real_escape_string构建安全的SQL语句
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: