How can I efficiently add a GET parameter with either a ? or & in PHP? -
i have add variable url. url might have variables. what's efficient way add new variable?
example urls:
http://domain.com/ http://domain.com/index.html?name=jones
i need add: tag=xyz
:
http://domain.com/?tag=xyz http://domain.com/index.html?name=jones&tag=xyz
what's efficient way know whether prepend string ? or &?
here's version of function have far:
// arradditions looks array('key1'=>'value1','key2'=>'value2'); function appendurlquerystring($url, $arradditions) { $arrquerystrings = array(); foreach ($arradditions $k=>$v) { $arrquerystrings[] = $k . '=' . $v; } $strappend = implode('&',$arrquerystrings); if (strpos($url, '?') !== false) { $url .= '&' . $strappend; } else { $url = '?' . $strappend; } return $url; }
but, looking ?
in existing url problematic? example, possible url includes ?
not actual queries, perhaps escaped character?
take @ php pecl's http_build_url. said doc page:
build url.
the parts of second url merged first according flags argument.
addition:
if don't have pecl installed, can jump through hoops. approach solid right until try rebuild new url. stock php (minus pecl) doesn't have reverse of parse_url()
. making harder, parse_url()
removes of grammar url in resulting parts array have put them in when reassemble. http_build_url()
can take care of us, if available, wouldn't reading portion it's recommended. anyway, here's code:
<?php /** * addqueryparam - given url , new params query string, return modified url * * @see http://us1.php.net/parse_url * @see http://us1.php.net/parse_str * @throws exception on bad input * @param string $url parseable url add query params * @param mixed $input_query_vars - string of & separated pairs of = separated key values or associative array of string keys => string values * @return string new url */ function addqueryparam ($url, $input_query_vars) { // parse new parameters if (is_string($input_query_vars)) { parse_str($input_query_vars, $input_query_vars); } // ensure array of parameters available if (!is_array($input_query_vars)) { throw new exception(__function__ . ' expects associative array or query string second parameter.'); } // break given url $url_parts = parse_url($url); // test proper url parse if (!is_array($url_parts)) { throw new exception(__function__ . ' expects parseable url first parameter'); } // produce array of original query vars $original_query_vars = array(); if (isset($url_parts['query']) && $url_parts['query'] !== '') { parse_str($url_parts['query'], $original_query_vars); } // merge new params inot original $new_query_vars = array_merge($original_query_vars, $input_query_vars); // replace original query string $url_parts['query'] = http_build_query($new_query_vars); // put url grammar in place if (!empty($url_parts['scheme'])) { $url_parts['scheme'] .= '://'; } if (!empty($url_parts['query'])) { $url_parts['query'] = '?' . $url_parts['query']; } if (!empty($url_parts['fragment'])) { $url_parts['fragment'] = '#' . $url_parts['fragment']; } // put , return return implode('', $url_parts); } // demo urls $url1 = 'http://domain.com/'; $url2 = 'http://domain.com/index.html?name=jones'; //some usage (i did cli) echo $url1, "\n"; echo addqueryparam($url1, 'tag=xyz'), "\n"; echo addqueryparam($url1, array('tag' => 'xyz')), "\n"; echo $url2, "\n"; echo addqueryparam($url2, 'tag=xyz'), "\n"; echo addqueryparam($url2, array('tag' => 'xyz')), "\n"; echo addqueryparam($url2, array('name' => 'foo', 'tag' => 'xyz')), "\n";
Comments
Post a Comment