How do I remove query strings from a url?
Example: I have a url such as the following:
http://example.com?q1=v1&q2=v2&q3=v3
I want to remove q1 and q3, leave q2, and add q4 with its value v4
How can I do this?
Kinda depends on if you know what the keys are or not. If you do, it'd be something like this:
$params = $_GET;
unset($params['q1']);
unset($params['q3']);
$params['q4'] = 'v4';
$my_new_param_string = http_build_query($params);
$new_url = 'http://example.com?' . $my_new_param_string;
You can manipulate $_GET directly, but I don't like to do that personally.
More info on http_build_query:
http://php.net/manual/en/function.http-build-query.php
If you just have that url as a string, you can do this instead:
$url_bits = parse_url('http://example.com/?q1=v1&q2=v2&q3=v3');
$params = parse_str($url_bits['query']);
And the rest works the same as the bit after $params = $_GET up above.
You can also use $url_bits to turn it back into a string later if you need to.
Related
I'd like to find a clean if possibile (without too much string manipulation preg_*)
I know that to replace a parameter I would do
$_GET['info'] = "newinfo";
and to remove a parameter:
unset($_GET['info']);
so is there something like that that I can use?
of course after I've "unset" or "set" I'm building a new query.
(http_build_query).
At the end I'm trying to make this:
/index.php?foo=bar
to
/index.php?foo=bar&info=newinfo
Just do this:
$get = $_GET;
$get['new'] = 'some value';
function getPath()
{
// Stolen from https://stackoverflow.com/a/8775529/3578036
$request = parse_url($_SERVER['REQUEST_URI']);
$path = $request["path"];
return rtrim(str_replace(basename($_SERVER['SCRIPT_NAME']), '', $path), '/');
}
header("Location: " . getPage() . http_build_query($get));
The above code will create a query string and append it to the current URL and redirect to that location. Obviously, you can change the location that you redirect to by replacing the getPage() function result and putting your own result there, this just demonstrates the premise of the answer.
The docs for http_build_query are a very good place to start.
Effectively, what it will do is convert an associative array into an HTTP query string.
i want to fetch youtube videos from the above script but the above code is getting keyword from GET parameter example.com/s=keyword and i want it to get from a example.com/HERE
i mean you can see there is a $_GET['s']
So this function works like this
example.com/s=keyword
and i want it to work like this
example/page/keyword
sorry for my bad english
$keyword = $_GET['s'];
file_get_contents("https://www.googleapis.com/youtube/v3/search?part=snippet&q=$keyword&type=video&key=abcdefg&maxResults=5");
Have a look at $_SERVER[REQUEST_URI]
This will return you the current url. Then process it using simple string or array functions to get the params, like
$current_url = $_SERVER['REQUEST_URI'];
$url_arr = explode("/", $current_url);
Then access the parameters using the array indexes
like $page = $url_arr[0];
Currently I have a url thats like this,
http://website.com/type/value
I am using
$url = $_SERVER['REQUEST_URI'];
$url = trim($url, '/');
$array = explode('/',$url);
this to get the value currently but my page has Facebook like's on it and when it is clicked it adds all these extra variables. http://website.com/type/value?fb_action_ids=1234567&fb_action_types= and that breaks that value that I am trying to get. Is there another way to get the specific value?
Assuming you know that this will always be a valid URL, you can use parse_url.
list(, $value) = explode('/', parse_url($url)['path']);
I'd use a preg_replace
explode('/', preg_replace('/?.*$/', '', $url));
You could also use:
$array = explode('/',$_SERVER['PATH_INFO']);
Or, this:
$array = explode('/',$_SERVER['PHP_SELF']);
With this, you do not need the trim() call or the temp var $url - unless you use it from something else.
The reason for two options is I don't know if /type/value is being passed to an index.php or if value is in fact a php file. Either way, one of the two options will give you what you need.
I have a url that gets encoded when serialized with jquery and comes out like this:
index.php?city%5B%5D=METROPLOIS&city%5B%5D=GOTHAM
Supposed to be index.php?city[]=METROPLOIS&city[]=GOTHAM
On the other end, I'm using joomla to pull down a bunch of variables out of the url
$city = JRequest::getVar('city');
Now, another user was nice enough to point me to this code in PHP which would get the city variables and then implode them into
$cities = $_GET['city'];
$str = "CITY='".implode("' OR CITY='",$city)."'";
Where $cities = $_GET['city']; is equivalent to $city = JRequest::getVar('city');
So, I'm running into a challenge that I don't know how to address. How to decode the URL in Joomla so that it will recognize city%5B%5D as city[] or city.
I've seen this: http://php.net/manual/en/function.urldecode.php, but that pulls down the URL and puts it into a string. How would I then tell Joomla to pull variables form that string instead of the URL ?
Use urldecode to decode the url
$decoded = urldecode('index.php?city%5B%5D=METROPLOIS&city%5B%5D=GOTHAM');
$query = parse_url($decoded, PHP_URL_QUERY);
parse_str($query, $params);
$city = $params['city'];
echo "CITY='".implode("' OR CITY='",$city)."'";
DEMO
http://php.net/manual/en/function.parse-url.php
http://php.net/manual/en/function.parse-str.php
Users can input URLs using a HTML form on my website, so they might enter something like this: http://www.example.com?test=123&random=abc, it can be anything. I need to extract the value of a certain query parameter, in this case 'test' (the value 123). Is there a way to do this?
You can use parse_url and parse_str like this:
$query = parse_url('http://www.example.com?test=123&random=abc', PHP_URL_QUERY);
parse_str($query, $params);
$test = $params['test'];
parse_url allows to split an URL in different parts (scheme, host, path, query, etc); here we use it to get only the query (test=123&random=abc). Then we can parse the query with parse_str.
I needed to check an url that was relative for our system so I couldn't use parse_str. For anyone who needs it:
$urlParts = null;
preg_match_all("~[\?&]([^&]+)=([^&]+)~", $url, $urlParts);
the hostname is optional but is required at least the question mark at the begin of parameter string:
$inputString = '?test=123&random=abc&usersList[]=1&usersList[]=2' ;
parse_str ( parse_url ( $inputString , PHP_URL_QUERY ) , $params );
print_r ( $params );