How PHP uses Http_build_query () to construct a URL string _php tips

Source: Internet
Author: User
Tags urlencode

This example describes how PHP uses Http_build_query () to construct a URL string. Share to everyone for your reference, specific as follows:

In short, http_build_query () is the conversion of an array to a URL question mark? The argument string that follows, and is automatically urlencode processed.

Or quote an official explanation:

Http_build_query

Http_build_query--Request string description after generation url-encoded string http_build_query (array formdata [, String Numeric_prefix])

Generates a url-encoded request string using the given associated (or subscript) array. Parameter formdata can be an array or an object that contains attributes. A formdata array can be either a simple one-dimensional structure or an array of arrays (which in turn can contain other arrays). If a numeric subscript is used in the underlying array and the Numeric_prefix parameter is given, this parameter value is prefixed to the numeric subscript element in the underlying array. This is to allow PHP or other CGI programs to obtain a valid variable name when decoding the data later.

Let's take a look at some simple examples:

$data = Array (
    ' foo ' => ' bar ', '
    baz ' => ' boom ',
    ' site ' => ' www.nowamagic.net ',
    ' name ' => ') Nowa Magic ');
echo Http_build_query ($data);
/* Output
 foo=bar&baz=boom&cow=milk&php=hypertext+processor
* *

What if it's an array of indexed arrays mixed with associative arrays?

$data = Array (
    ' foo ',
    ' bar ', '
    site ' => ' www.nowamagic.net ', '
    name ' => ' Nowa Magic ');
echo Http_build_query ($data);
/* Output
    0=foo&1=bar&site=www.nowamagic.net&name=nowa+magic
* *

It automatically adds a numeric index.

Http_build_query also has a parameter that can prefix the numeric index, let's try again:

$data = Array (
    ' foo ',
    ' bar ', '
    site ' => ' www.nowamagic.net ', '
    name ' => ' Nowa Magic ');
Echo http_build_query ($data, "nm_");
/* Output
nm_0=foo&nm_1=bar&site=www.nowamagic.net&name=nowa+magic
* *

What about the more complex arrays? Like a two-dimensional array or something.

$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 ';

Its output results are:

user%5bname%5d=bob+smith&user%5bage%5d=47&user%5bsex%5d=m&user%5bdob%5d=5%2f12%2f1956
& Pastimes%5b0%5d=golf&pastimes%5b1%5d=opera&pastimes%5b2%5d=poker
&pastimes%5B3%5D=rap& Children%5bbobby%5d%5bage%5d=12&children%5bbobby%5d%5bsex%5d=m
&children%5bsally%5d%5bage%5d=8 &children%5bsally%5d%5bsex%5d=f&0=ceo

The line is folded for readability:

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

Note: Only the digital subscript element "CEO" in the underlying array gets the prefix, and other numeric subscript elements (such as elements under pastimes) do not need to prefix the valid variable names.

Not just an array, but even an object can be converted to a URL string:

Class MyClass {
  var $foo;
  var $baz;
  function MyClass () {
  $this->foo = ' bar ';
  $this->baz = ' boom ';
  }
}
$data = new MyClass ();
echo Http_build_query ($data);

At the end, a few more functions are probably what you need to know when you search for http_build_query:

1. Parse_str: Converts a URL back to an array of parameters, array parse_str (Url,arr).

2. Parse_url: Resolves a full URL to an array parse_url (string url).

3. Http_build_query: Again, briefly explain, convert an array to a URL? The argument string that follows is UrlEncode processed automatically, string http_build_query (array formdata [, String Numeric_prefix]), followed by a subscript that does not specify a key in the array or the key is a number.

More interested in PHP related content readers can view the site topics: "PHP Operations and Operator Usage Summary", "PHP Network Programming Skills Summary", "PHP Basic Grammar Introductory Tutorial", "PHP operation Office Document Skills Summary (including word,excel,access, PPT), "The PHP date and time usage summary", "PHP object-oriented Programming Introduction Course", "PHP string (String) Usage Summary", "Php+mysql Database operation Introduction Tutorial" and "PHP common database Operation skill Summary"

I hope this article will help you with the PHP program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.