append query string to any form of URL
<?php
$urls = array(
'http://www.example.com',
'http://www.example.com/a/',
'http://www.example.com/a/?q1=one',
'http://www.example.com/a.html',
'http://www.example.com/a.html?q1=one'
);
$query = 'q2=two';
foreach($urls as &$url) {
$parsedUrl = parse_url($url);
if ($parsedUrl['path'] == null) {
$url .= '/';
}
$separator = ($parsedUrl['query'] == NULL) ? '?' : '&';
$url .= $separator . $query;
}
var_dump($urls);
Output
array(5) {
[0]=>
string(29) "http://www.example.com/?q2=two"
[1]=>
string(32) "http://www.example.com/a/?q2=two"
[2]=>
string(39) "http://www.example.com/a/?q1=one&q2=two"
[3]=>
string(36) "http://www.example.com/a.html?q2=two"
[4]=>
&string(43) "http://www.example.com/a.html?q1=one&q2=two"
}
CodePad.
$url is your URL. Use strpos function
if(strpos($url,'?') !== false) {
$url .= '&q2=two';
} else {
$url .= '?q2=two';
}
I know this is old, but I improved alex's answer to account for the "#" part of the string.
$urls = array(
'http://www.example.com',
'http://www.example.com/a/#something',
'http://www.example.com/a/?q1=one#soe',
'http://www.example.com/a.html',
'http://www.example.com/a.html?q1=one'
);
$query = 'q2=two';
foreach($urls as &$url) {
$pound = "";
$poundPos = -1;
//Is there a #?
if ( ( $poundPos = strpos( $url, "#" ) ) !== false )
{
$pound = substr( $url, $poundPos );
$url = substr( $url, 0, $poundPos );
}
$separator = (parse_url($url, PHP_URL_QUERY) == NULL) ? '?' : '&';
$url .= $separator . $query . $pound;
}
var_dump($urls);
This is the function I use:
/**
* @param string $url
* @param $query string|array
* @return string
*/
public function appendQueryStringToURL(string $url, $query): string
{
// the query is empty, return the original url straightaway
if (empty($query)) {
return $url;
}
$parsedUrl = parse_url($url);
if (empty($parsedUrl['path'])) {
$url .= '/';
}
// if the query is array convert it to string
$queryString = is_array($query) ? http_build_query($query) : $query;
// check if there is already any query string in the URL
if (empty($parsedUrl['query'])) {
// remove duplications
parse_str($queryString, $queryStringArray);
$url .= '?' . http_build_query($queryStringArray);
} else {
$queryString = $parsedUrl['query'] . '&' . $queryString;
// remove duplications
parse_str($queryString, $queryStringArray);
// place the updated query in the original query position
$url = substr_replace($url, http_build_query($queryStringArray), strpos($url, $parsedUrl['query']), strlen($parsedUrl['query']));
}
return $url;
}
It accepts query
as string
or array
. Also it handles #
in URL and removes the duplicated query strings automatically. Here is the test as well. Please replace CLASS_THAT_CONTAINS_appendQueryStringToURL
with the correct class
in your project:
public function testAppendQueryStringToURL()
{
$helper = new CLASS_THAT_CONTAINS_appendQueryStringToURL();
$inputsOutputs = [
[
'i' => 'http://www.example.com',
'q' => 'q1=1',
'o' => 'http://www.example.com/?q1=1'
],
[
'i' => 'http://www.example.com',
'q' => 'q1=1&q2=2',
'o' => 'http://www.example.com/?q1=1&q2=2'
],
[
'i' => 'http://www.example.com/a/',
'q' => 'q1=1',
'o' => 'http://www.example.com/a/?q1=1'
],
[
'i' => 'http://www.example.com/a.html',
'q' => 'q1=1',
'o' => 'http://www.example.com/a.html?q1=1'
],
[
'i' => 'http://www.example.com/a/?q2=2',
'q' => 'q1=1',
'o' => 'http://www.example.com/a/?q2=2&q1=1'
],
[
'i' => 'http://www.example.com/a.html?q2=two',
'q' => 'q1=1',
'o' => 'http://www.example.com/a.html?q2=two&q1=1'
],
[
'i' => 'http://www.example.com/a.html?q1=1&q2=2',
'q' => 'q1=1',
'o' => 'http://www.example.com/a.html?q1=1&q2=2'
],
// overwrite the existing
[
'i' => 'http://www.example.com/a.html?q1=1&q2=2',
'q' => 'q1=3',
'o' => 'http://www.example.com/a.html?q1=3&q2=2'
],
[
'i' => 'http://www.example.com/a/#something',
'q' => 'q1=1',
'o' => 'http://www.example.com/a/#something?q1=1'
],
[
'i' => 'http://www.example.com/a/?q2=2#soe',
'q' => 'q1=1',
'o' => 'http://www.example.com/a/?q2=2&q1=1#soe'
],
[
'i' => 'http://www.example.com',
'q' => ['q1' => 1],
'o' => 'http://www.example.com/?q1=1'
],
[
'i' => 'http://www.example.com',
'q' => ['q1' => 1, 'q2' => 2],
'o' => 'http://www.example.com/?q1=1&q2=2'
],
[
'i' => 'http://www.example.com/a/',
'q' => ['q1' => 1],
'o' => 'http://www.example.com/a/?q1=1'
],
[
'i' => 'http://www.example.com/a.html',
'q' => ['q1' => 1],
'o' => 'http://www.example.com/a.html?q1=1'
],
[
'i' => 'http://www.example.com/a/?q2=2',
'q' => ['q1' => 1],
'o' => 'http://www.example.com/a/?q2=2&q1=1'
],
[
'i' => 'http://www.example.com/a.html?q2=two',
'q' => ['q1' => 1],
'o' => 'http://www.example.com/a.html?q2=two&q1=1'
],
[
'i' => 'http://www.example.com/a.html?q1=1&q2=2',
'q' => ['q1' => 1],
'o' => 'http://www.example.com/a.html?q1=1&q2=2'
],
[
'i' => 'http://www.example.com/a.html?q1=1&q2=2',
'q' => ['q1' => 3],
'o' => 'http://www.example.com/a.html?q1=3&q2=2'
],
[
'i' => 'http://www.example.com/a/#something',
'q' => ['q1' => 1],
'o' => 'http://www.example.com/a/#something?q1=1'
],
[
'i' => 'http://www.example.com/a/?q2=2#soe',
'q' => ['q1' => 1],
'o' => 'http://www.example.com/a/?q2=2&q1=1#soe'
],
];
foreach ($inputsOutputs as $inputOutput) {
$this->assertEquals($inputOutput['o'], $helper->appendQueryStringToURL($inputOutput['i'], $inputOutput['q']));
}
}