Getting second last parameter from querystring with PHP [duplicate] - php

This question already has answers here:
Getting parts of a URL (Regex)
(30 answers)
Closed 3 years ago.
I have a URL like so:
http://example.com/var1/var2
What I'd like to is get VAR 1 from the querystring. I've tried using a regex but I'm not really very familiar with them. Is there an easier way?

Another approach
<?php
$e = explode("/",parse_url("http://example.com/a/b")["path"])[1];
// $e now is "a"

Related

Get portion of URL with PHP [duplicate]

This question already has answers here:
Get parts of URL in PHP
(4 answers)
Closed 1 year ago.
I had previously been using basename to grab the last part of my URL however have noticed some issues if my URL contains parameters.
So if my URL is this:
https://www.google.com/test-page/?utm_source=test
How would I pull test-page from this?
You split it by the / delimiter, and then take the fourth item
$link = 'https://www.google.com/test-page/?utm_source=test';
$split = explode('/', $link);
if(isset($split[3]))
{
echo $split[3];
}

Get part of string PHP [duplicate]

This question already has answers here:
PHP - parse current URL
(6 answers)
Closed 2 years ago.
How to get a part of string using PHP?
I have a string like this.
https://test-app.com/admin/api/2019-10/orders.json?limit=2&page_info=eyJpZHMiOiIyMDY3MTczMTEzOTg3LDIwNjU0ODU0MzA5MTUsMjA2NTQ3OTI3MDUzMSwyMDYyODE3MzI5MjgzIiwibGFzdF9pZCI6MjA2NTQ4NTQzMDkxNSwibGFzdF92YWx1ZSI6IjIwMjAtMDMtMTcgMTg6MTc6NTkiLCJkaXJlY3Rpb24iOiJuZXh0In0
I want only the link.. like this
orders.json?limit=2&page_info=eyJpZHMiOiIyMDY3MTczMTEzOTg3LDIwNjU0ODU0MzA5MTUsMjA2NTQ3OTI3MDUzMSwyMDYyODE3MzI5MjgzIiwibGFzdF9pZCI6MjA2NTQ4NTQzMDkxNSwibGFzdF92YWx1ZSI6IjIwMjAtMDMtMTcgMTg6MTc6NTkiLCJkaXJlY3Rpb24iOiJuZXh0In0
That's not just a string it's a URL so use URL and path functions:
$parts = parse_url($str);
echo basename($parts['path']).$parts['query'];

replace particular value from url [duplicate]

This question already has answers here:
PHP, Delete parts of a URL variable [duplicate]
(3 answers)
Closed 4 years ago.
I want to replace '/50' with URL.my URL is
http://localhost/CI/admin/pcustomer/cprofile/50?customer=18267&mobile=&nearby=
I want url as
http://localhost/CI/admin/pcustomer/cprofile?customer=18267&mobile=&nearby=
From that little info in your question, this seems to be the simplest way to achieve "I want to replace '/50' with URL"
$url = str_replace('/50', '', 'http://localhost/CI/admin/pcustomer/cprofile/50?customer=18267&mobile=&nearby=');

How can I get URL (with some parameters)? [duplicate]

This question already has answers here:
Get URL query string parameters
(11 answers)
Closed 6 years ago.
I have a form on page, with url like this:
site.com/form.index.html?ref=https://login....
And I get page URL via this code:
http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]
But as result I get not full URL, just this:
site.com/form.index.html
How can I get full URL?
You want to add the query string:
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]$_SERVER[QUERY_STRING]";
Additionally, you may replace the http bit with $_SERVER[REQUEST_SCHEME]:
"$_SERVER[REQUEST_SCHEME]://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]$_SERVER[QUERY_STRING]"

How to get a URL query next to the "#!" prefix? [duplicate]

This question already has answers here:
Get fragment (value after hash '#') from a URL [closed]
(10 answers)
Closed 7 years ago.
I'm trying to get the URL query next the "#!" prefix, for example:
https://mega.co.nz/#!123abc
<?php
$query=$_GET["#!"]; //the example
?>
$query: 123abc.
It is possible to read the query using PHP or .htaccess file? How i can get started?
Thanks for reading and for any help! :)
Try this,
Let your URL:
https://mega.co.nz?query=123abc
Then you can get value as below :
<?php
$query=$_GET["query"]; //the example
?>

Categories