php url函数

时间:2023-03-09 20:19:28
php url函数

1、base64_encode 与 base64_decode

base64_encode(string) 表示使用 MIME base64 对数据进行编码

base64_decode(string)表示对编码后的字符串进行解码

<?php
header('Content-type:text/html;charset=utf8');
$str = 'are you ok???';
$secret = base64_encode($str);
echo $secret;
//输出 YXJlIHlvdSBvaz8/Pw==
echo base64_decode($secret);
//输出 are you ok???
?>

2、urlencode 与 urldecode(在使用低版本的浏览器时容易产生乱码,可以用这个进行先转换)

urlencode  编码url字符串

urldecode  解码url字符串

<?php
header('Content-type:text/html;charset=utf8');
$str = 'http://php.net/manual/zh/function.urlencode.php';
$url = urlencode($str);
echo $url;
//输出 http%3A%2F%2Fphp.net%2Fmanual%2Fzh%2Ffunction.urlencode.php
echo urldecode($url);
//输出 http://php.net/manual/zh/function.urlencode.php
?>

3、get_headers

get_headers('http://...')抓取http头信息

<?php
header('Content-type:text/html;charset=utf8');
$arr = get_headers('http://www.baidu.com');
var_dump($arr);
//输出一个关于百度的相关头信息,为一个数组
?>

4、rawurlencode 与rawurldecode

用法和urlencode一样,唯一的不同是对空格的处理,urlencode处理成“+”,rawurlencode处理成“%20”。

<?php
header('Content-type:text/html;charset=utf8');
$str = 'http://www.baidu .com';
echo urlencode($str);
//输出 http%3A%2F%2Fwww.baidu+.com
$url = rawurlencode($str);
echo $url;
//输出 http%3A%2F%2Fwww.baidu%20.com
echo rawurldecode($str);
//输出 http://www.baidu .com
?>

5、parse_url

parse_url — 解析 URL,返回其组成部分.如果是不合法的url,那么就返回false

<?php
header('Content-type:text/html;charset=utf8');
$str = 'https://i.cnblogs.com/EditPosts.aspx?postid=9761891&update=1';
var_dump(parse_url($str));
//输出值 array(4) { ["scheme"]=> string(5) "https" ["host"]=> string(13) "i.cnblogs.com" ["path"]=> string(15) "/EditPosts.aspx" ["query"]=> string(23) "postid=9761891&update=1" }
?>

6、http_build_query

这里暂不讨论该函数的其他参数,把数组通过一定的规则拼接起来

<?php
header('Content-type:text/html;charset=utf8');
$arr = [
'name' => 'aaa',
'age' => 30,
'sex' => 'man',
'hobby' => 'computer'
];
var_dump(http_build_query($arr));
//输出 string(38) "name=aaa&age=30&sex=man&hobby=computer"
?>

7、get_meta_tags

get_meta_tags — 从一个文件中提取所有的 meta 标签 content 属性,返回一个数组

8、从ajax的put与delete类型获取数据

parse_str(file_get_contents('php://input'), $arr);