欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

http_build_query 比较好用的字符串函数

程序员文章站 2022-06-03 21:13:34
...
http_build_query 比较好用的字符串函数
<?php
/*
* http_build_query 
* 是一个字符串操作函数
*针对的是数组和对象生成 URL-encode 之后的请求字符串
*
*/

$arr = array("121212"=>"woman","56856"=>"men");
class jclass{
public $a=123;
private $b=323;
protected $c = 897;
}

$str = http_build_query($arr);//string(22)"121212=woman&56856=men"

var_dump($str);
echo"<br>";
$jclass = new jclass();
$jclass = json_encode($jclass);//string(9)"{"a":123}"
var_dump($jclass);

echo"<br>";
echo http_build_query($arr,'',"_kkkkk");
echo"<br>";
var_dump(http_build_query($arr,"kkk_"));
echo"<br>";



$data = array('user'=>array('name'=>'Bob Smith',
'age'=>47,
'sex'=>'M',
'dob'=>'5/12/1956'),
 'pastimes'=>array('golf', 'opera', 'poker', 'rap'),
'children'=>array('bobby'=>array('age'=>12,
'sex'=>'M'),
'sally'=>array('age'=>8,
'sex'=>'F')),
'CEO');

echo http_build_query($data, 'flags_'); echo"<br>";

// http://dev.blogs/string/http_build_query.php?user[name]=Bob+Smith&user[age]=47&user[sex]=M&user[dob]=5%2F12%2F1956&pastimes[0]=golf&pastimes[1]=opera&pastimes[2]=poker&pastimes[3]=rap&children[bobby][age]=12&children[bobby][sex]=M&children[sally][age]=8&children[sally][sex]=F&flags_0=CEO
$get_arr = $_GET;
print_r($get_arr); echo"<br>";//呵呵,过来的已经是数组了
$cval ="something from somewhere";
setcookie("test_cookie", $cval, time() + 60 * 60 * 24);
var_dump($_COOKIE);echo '<br>';
//var_dump(http_build_cookie($_COOKIE));//报错,是因为 pecl_http 扩展是没有默认安装的,要先去安装扩展 php_http.dll