Home > php教程 > PHP源码 > http_build_query 比较好用的字符串函数

http_build_query 比较好用的字符串函数

PHP中文网
Release: 2016-05-26 08:20:34
Original
1187 people have browsed it

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,&#39;&#39;,"_kkkkk");
echo"<br>";
var_dump(http_build_query($arr,"kkk_"));
echo"<br>";



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

echo http_build_query($data, &#39;flags_&#39;); 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 &#39;<br>&#39;;
//var_dump(http_build_cookie($_COOKIE));//报错,是因为 pecl_http 扩展是没有默认安装的,要先去安装扩展 php_http.dll
Copy after login
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template