Current page url in PHP - php

I've this url:
http://localhost/alignment/?page_id=52&q=2
I want to get the portion: ?page_id=52 , how can I do that?

$_SERVER['QUERY_STRING']
or
$url = "http://localhost/alignment/?page_id=52&q=2";
$bits = explode("?", $url);
$querystring = $bits[1]; // this is what you want
but the first one will be much more reliable and is easier. :)
EDIT
if you meant that you just wanted that one variable use:
$_GET['page_id']

This is called a query string. The main portion of the query string is separated by the rest of the URL with a ?. Each argument in the query string is separated by a &.
PHP:
$_SERVER['QUERY_STRING'];
If you want to get the individual pieces, use:
$_GET['page_id']; //etc...

You can get the whole query string with $_SERVER['QUERY_STRING'], but you would have to parse out page_id part. If you insist on doing things manually the function parse_str may come in handy.
A better choice would be to just use the predefined $_GET global variable. $_GET['page_id'] would give you value 52.

If you have it as a string, use parse_url. Documentation here. Get the query value of it. or if its current request use $_SERVER['QUERY_STRING']

echo parse_url($url,PHP_URL_QUERY);

This should do it:
echo $_SERVER['QUERY_STRING'];

Assign the url to string and then explode() it,
http://php.net/manual/en/function.explode.php, using the ? as a delimiter

Related

PHP Explode Remove "Extra" Text

Assuming I have the following URL on a webpage, how I can I utilize the explode() function to give me only the ID, and not the rest of the URL that follows the ID?
file.php?id=12345&foo=bar
I can get the ID, but there's always the following "&foo=bar".
Thanks.
If you need to treat a valid URL, you can use
parse_url
parse_url documentation
to explode the URL into different part (SCHEME, HOST, PORT, USER...)
and then, use
parse_str
parse_str documentation
on the QUERY part, in order to retrieve an array containing all your parameters.
Then, you can catch what you need.
parse_str($_SERVER['QUERY_STRING']);
echo $id;
parse_str will create PHP-variables from query-string variables.
http://www.php.net/manual/en/function.parse-str.php
If it's a full URL, you can use parse_url() to get the query string part.
If it's just a fragment, you can use explode() to get it, then parse_str():
list($path, $qs) = explode('?', $url, 2);
parse_str($qs, $args);
echo $args['id'];
The 2 tells explode() to break the string into a maximum of 2 parts (before and after the ?, in this case).

Get last word from URL after equals sign

I can't find a solution for this problem. I have this URL http://examplesite.com/?skill-type=new and I want to get the very last word after the = sign, using PHP only. "skill-type" stays the same all the time Thanks in advance :)
This snippet should do:
$url = 'http://examplesite.com/?skill-type=new';
$skillType = explode('&', parse_url($url, PHP_URL_QUERY))['skill-type'];
Check parse_url for more details.
If you meant that the request comes in to that particular url, just use $_REQUEST['skill-type'] to retrieve the value.
Considering you already have the URL in a variable, let's say
$url = 'http://examplesite.com/?skill-type=new;
One possible way (certainly there are others) would be:
$urlArray = explode('=',$url);
$last = $urlArray[sizeof($urlArray)-1];
Note:
only applicable if you don't know how the URL comes, if you do, consider on using $_GET
Use the PHP $_GET Global Variable
$skill_type = $_GET['skill-type'];
PHP will automatically parse GET variables for you. To access the value of the skill-type variable, you need only use $_GET['skill-type']. For instance:
echo $_GET['skill-type'];
Will display
new
Generate your url by $_SERVER variable and then split it like this
$url = "http://examplesite.com/?skill-type=new";
$array = explode("=", $url);
$last_word = $array[1];
Use substr (url, posstr (url,'=')+1) or if this is the page being loaded $_GET ['skill-type']

Capture value from URL string with php

I need to extract a variable's value from a string, which happens to be a URL. The string/url is loaded as part of a separate php query, not the url in the browser.
The url's will look like:
index.php?option=com_content&view=article&catid=334:golfeq&id=2773:xcelsiors&Itemid=44
How can I always find & capture the value of the id which in this example is 2773?
I read several examples already, but what I've tried captures the id value of the current page which is being viewed in the browser, and not the URL string.
Thanks
You are looking for a combination or parse_url (which will isolate the query string for you) and parse_str (which will parse the variables and put them into an array).
For example:
$url = 'index.php?option=com_content&view=article&catid=334:golfeq&id=2773:xcelsiors&Itemid=44';
// This parses the url for the query string, and parses the vars from that
// into the array $vars (which is created on the spot).
parse_str(parse_url($url, PHP_URL_QUERY), $vars);
print_r($vars); // see what's in there
// Parse the value "2773:xcelsiors" to isolate the number
$id = reset(explode(':', $vars['id']));
// This will also work:
$id = intval($vars['id']);
echo "The id is $id\n";
See it in action.
You can use parse_str
You can use parse_url to parse URLs!
But you can use, to extract directly the numbers of ID variable:
$url = 'index.php?option=com_content&view=article&catid=334:golfeq&id=2773:xcelsiors&Itemid=44';
$id = preg_replace("/^.*[&?]id=([0-9]+).*$/",'$1',$url);
echo $id;

How to change variables in link of foo?q=some&s=3&d=new

Consider a php script visited with URL of foo?q=some&s=3&d=new. I wonder if there is a paractical method for parsing the url to create links with new variable (within php page). For example foo?q=**another-word**&s=3&d=new or foo?q=another-word&s=**11**&d=new
I am thinking of catching the requested URL by $_SERVER['REQUEST_URI'] then parsing with regex; but this is not a good idea in practice. There should be a handy way to parse variables attached to the php script. In fact, inverse action of GET method.
The $_GET variable contains an already parsed array of the current query string. The array union operator + makes it easy to merge new values into that. http_build_query puts them back together into a query string:
echo 'foo?' . http_build_query(array('q' => 'another-word') + $_GET);
If you need more parsing of the URL to get 'foo', use parse_url on the REQUEST_URI.
What about using http_build_query? http://php.net/manual/en/function.http-build-query.php
It will allow you to build a query string from an array.
I'd use parse_str:
$query = 'q=some&s=3&d=new';
parse_str($query, $query_parsed);
$query_parsed['q'] = 'foo-bar';
$new_query = implode('&', array_map(create_function('$k, $v',
'return $k."=".urlencode($v);'),
array_keys($query_parsed), $query_parsed));
echo $new_query;
Result is:
q=foo-bar&s=3&d=new
Although, this method might look like "the hard way" :)

How can I replace a variable in a get query in PHP?

I have an URL
http://example.com/test?xyz=27373&page=4&test=5
which I want to tranform by replacing the page=4 through page=XYZ
how can I do that with preg_replace?
Yes, you can use
$oldurl = "http://test.com/test?xyz=27373&page=4&test=5"
$newurl = preg_replace("/page=\d+/", "page=XYZ", $oldurl);
Or you can reconstruct the URL from $_GET superglobal.
Do you want to set the value of xyz to the page value? I think you might need to specify a bit more. But this is easy to modify if you dont know regex.
$url = 'http://test.com/test?xyz=27373&page=4&test=5';
$urlQuery = parseUrl($url, PHP_URL_QUERY);
parse_str($urlQuery, $queryData);
$queryData['page'] = $queryData['xyz'];
unset($queryData['xyz']);
$query = http_build_query($queryData);
$outUrl = substr_replace($url, $query, strpos($url, '?'));
$url = 'http://test.com/test?xyz=27373&page=4&test=5';
preg_match('/xyz=([^&]+)/', $url, $newpage);
$new = preg_replace('/page=([^&]+)/', $newpage[0], $url);
$new = preg_replace('/xyz=([^&]+)&/', '', $new);
This will turn
http://test.com/test?xyz=27373&page=4&test=5
into
http://test.com/test?page=27373&test=5
Forgive me if this isn't what you were looking to do, but your question isn't quite clear.
I'm sure you could do something with a regular expression. However, if the URL you've given is the one you're currently handling, you already have all the request variables in $_Request.
So, rebuild the URL, replacing the values you want to replace, and then redirect to the new URL.
Otherwise, go find a regexp tutorial.
If this is your own page (and you are currently on that page) those variables will appear in a global variable named $_GET, and you could use something like array_slice, unset or array_filter to remove the unwanted variables and regenerate the URL.
If you just have that URL as a string, then what exactly are the criteria for removing the information? Technically there's no difference between
...?xyz=27373&page=4&test=5
and
...?test=5&xyz=27373&page=4
so just removing all but the first parameter might not be what you want.
If you want to remove everything except the xyz param. Take a look at parse_url and parse_str
What exactly are you trying to do? The question is a little unclear.
$XYZ = $_GET['xyz'];
$PAGE = $_GET['page'];
?
Are wanting to replace each value with another, or replace both with one?

Categories