url格式化函數http_build_query() 和parse_str() 函數

來源:互聯網
上載者:User

標籤:元素   bsp   style   字元   mit   cow   nbsp   amp   字串   

例子 1. http_build_query() 使用樣本

<?php
$data = array(‘foo‘=>‘bar‘,
‘baz‘=>‘boom‘,
‘cow‘=>‘milk‘,
‘php‘=>‘hypertext processor‘);
echo http_build_query($data);
/* 輸出:
foo=bar&baz=boom&cow=milk&php=hypertext+processor
*/
?>

例子 2. http_build_query() 使用數字下標的元素

<?php
$data = array(‘foo‘, ‘bar‘, ‘baz‘, ‘boom‘, ‘cow‘ => ‘milk‘, ‘php‘ =>‘hypertext processor‘);
echo http_build_query($data);
/* 輸出:
0=foo&1=bar&2=baz&3=boom&cow=milk&php=hypertext+processor
*/
echo http_build_query($data, ‘myvar_‘);
/* 輸出:
myvar_0=foo&myvar_1=bar&myvar_2=baz&myvar_3=boom&cow=milk&php=hypertext+processor
*/
?>

例子 3. http_build_query() 使用複雜的數組

<?php
$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_‘);
/* 輸出:(為了可讀性對其進行了折行)
user[name]=Bob+Smith&user[age]=47&user[sex]=M&user[dob]=5%1F12%1F1956&
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
注意:只有基礎數組中的數字下標元素“CEO”才擷取了首碼,其它數字下標元素(如
pastimes 下的元素)則不需要為了合法的變數名而加上首碼。
*/
?>

例子 4. http_build_query() 使用對象

<?php
class myClass {
var $foo;
var $baz;
function myClass() {
$this->foo = ‘bar‘;
$this->baz = ‘boom‘;
}
}
$data = new myClass();
echo http_build_query($data);
/* 輸出:
foo=bar&baz=boom
*/
?>

 

把查詢字串解析到變數中:

<?phpparse_str("name=Bill&age=60");echo $name."<br>";echo $age;?>

在數組中儲存變數:

<?phpparse_str("name=Bill&age=60",$myArray);print_r($myArray);?>

url格式化函數http_build_query() 和parse_str() 函數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.