Recipe 8.6. Building a Query String8.6.1. ProblemYou 8.6.2. SolutionUse the http_build_query( ) Building a query string
8.6.3. DiscussionThe URL built in Example 8-14 is: /muppet/select.php?name=Oscar+the+Grouch&color=green&favorite_punctuation=%23 The query string has spaces encoded as Although the encoding that http_build_query( ) does prevents any special characters in the variable names or values from disrupting the constructed URL, you may have problems if your variable names begin with the names of HTML entities. Consider this partial URL for retrieving information about a stereo system: /stereo.php?speakers=12&cdplayer=52&=10 The HTML entity for /stereo.php?speakers=12&cdplayer=52&=10 To prevent embedded $url = '/muppet/select.php?' . htmlentities($query_string); The resulting URL is: /muppet/select.php?name=Oscar+the+Grouch&color=green&favorite_punctuation=%23 Your third choice is to change the argument separator from & to & by setting the configuration directive arg_separator.input to &. Then, http_build_query( ) joins the different name=value pairs with &: /muppet/select.php?name=Oscar+the+Grouch&color=green&favorite_punctuation=%23 8.6.4. See AlsoDocumentation on http_build_query( ) at http://www.php.net/http_build_query and htmlentities( ) at http://www.php.net/htmlentities. |
Thursday, February 4, 2010
Recipe 8.6. Building a Query String
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment