PHP - Get the end of a URL - php

So I think I need to submit a new question for this...
Here is my old question: PHP - Get path minus root
I need a way in PHP to take the URL being any of the following...
http://kenthomes.net/plan_detail.php?mod=39
http://kenthomes.net/Amelia-Cove
and get everything after leaving me with...
"plan_detail.php?mod=39" // If there is no alias for that page
OR
"Amelia-Cove" // If that page has an alias being applied
In reality, they are the same page, because of the alias, but not all of these pages have aliases associated with them such as...
http://kenthomes.net/plan_detail.php?mod=52
unlike...
http://kenthomes.net/Amelia-Cove
Currently I am using...
trim($_SERVER['REQUEST_URI'],'/')
which gives me...
"Amelia-Cove" // Which is fine.
OR
"plan_detail.php" // Which is not okay.
I need..
"Amelia-Cove" // Which is fine.
OR
"plan_detail.php?mod=39" // Which is fine.
How do I do this?

You can get all the parts of an URL via parse_url()
For example;
$parts = parse_url('http://kenthomes.net/plan_detail.php?mod=39');
print_r($parts);
Should give you something like this:
Array
(
[scheme] => http
[host] => kenthomes.net
[path] => /plan_detail.php
[query] => mod=39
)
Which you can use to create your own URL containing the parts that you need

$_SERVER["REQUEST_URI"] only contains the URI.
When you also want the part after the ?, you need to also use $_SERVER["QUERY_STRING"].
Use:
trim($_SERVER['REQUEST_URI'] . $_SERVER['QUERY_STRING'], '/');

Append $_SERVER['REQUEST_URI'] with $_SERVER['QUERY_STRING'].
PHP: $_SERVER - Manual

You can get the query string (the bit after the question mark), via $_SERVER['QUERY_STRING']

parse_url, and if you just want the far right, using str_split would be sufficient.
$data = parse_url($url, PHP_URL_PATH);
Should be enough.
Otherwise if responding to the current request, $_SERVER['REQUEST_URI'] might work, as that is the entire URI.

You can try this:
$uri = $_SERVER['REQUEST_URI'];
$qs = $_SERVER['QUERY_STRING'];
echo trim($uri . $qs, '/');

Related

Create a php variable from URL without question mark

I need to create a variable in PHP from a URL, which does not have a fully formed query string.
e.g. http://search.domain.com/domain2.com
In this example, the variable needs to be
$website='domain2.com'
Is there a way to convert the entered URL in address bar to my ?website= variable?
An example would be the whois.domaintools service, which allows you to query a whois record from their website using the following url format:
http://whois.domaintools.com/domain.com
This then displays info based on the url you specified.
Can i achieve this using a MOD_Rewrite in the .htaccess, or can i use some PHP function like http_build_query to achieve this? I'm going around in circles and surely missing something obvious!
You can use this code to get your array $urlpart
$link = $_SERVER['REQUEST_URI'];
$urlpart = explode('/',trim(parse_url($link, PHP_URL_PATH), '/'));

PHP : I need to read value from ur "www.smple.com/sample.php/value in php

I need to read the value from url in php code. Below is the sample url . I am passing the value after php file name.
I know that we can use query string like "www.smple.com/sample.php?name=value"
But requirement is url need to filename/value, this value will be dynamic.
Please any one can help me to solve this issue.
URL : www.smple.com/sample.php/value
Use parse_url to get the path from the URL and then use explode to split it into its segments.
$uri_path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$uri_segments = explode('/', $uri_path);
echo $uri_segments[1];
If the value is like
http://www.example.com?value=42
You can read it with
$value = $_GET['value'];
If the value is like
http://www.example.com/index.php/foo
where foo is the value, you can read it with $_SERVER variable:
$_SERVER['SERVER_NAME'] => localhost
$_SERVER['REQUEST_URI'] => /index.php/foo
$_SERVER['REQUEST_METHOD'] => GET
$_SERVER['SCRIPT_NAME'] => /index.php
$_SERVER['SCRIPT_FILENAME'] => /Users/sensorario/Development/local/fake-server/web/index.php
$_SERVER['PATH_INFO'] => /foo
$_SERVER['PHP_SELF'] => /index.php/foo
And also, if you need more information about a request, .. just type
print_r($_SERVER);
to see all the information you can get from current request ^_^
If i get it right, you want to pass your param as a friendly url. If thats the case fisrt you need to use the re-write mode in either Apache or Nginx so when you get example.com/xxx/yyy it sends to php example.com/xxx?param=yyy after doing it you can get it with $_SERVER['QUERY_STRING'] as mentioned in answers above/below.
Apache info: http://httpd.apache.org/docs/current/mod/mod_rewrite.html

Using link data as PHP variable input

I have a link. Eg. abc.com/qwerty. I want to extract the part after / of every input just like examples below and use it just like a PHP GET input value and store it to a variable $page. Essentially, the link abc.com/qwerty should work like abc.com/proc.php?x=qwerty
Typed link Part to be used as PHP GET input
abc.com/cvbx cvbx
abc.com/ghvs ghvx
abc.com/pabc pabc
How can I do this?
You can use: $_SERVER['REQUEST_URI']
$request_uri = $_SERVER['REQUEST_URI']; //returns '/cvbx'
$segments = array_filter(explode('/', $request_uri)); //array_filter to remove empty elements.
You can parse the url as described at http://php.net/manual/en/function.parse-url.php .
If you have urls as described there, you can get the path part with:
$input = substr(parse_url($url, PHP_URL_PATH),1);
substr is to remove the starting /

PHP - matching and replacing urls

If I have multiple urls like this in a variable that a user enters called $url:
http://www.example.com/home
/home
?home=true
home/
And it's all supposed to be under the www.example.com website,
How can I replace the urls to their right form?
Something like this :
http://www.example.com/home => http://www.example.com/home
/ihome => http://www.example.com/ihome
?home=true => http://www.example.com/ihome?home=true
home/ => http://www.example.com/ihome/home/
Both last with current page to /ihome.
I'm assuming your question is about relative URLs.
Suppose you are at location http://example.com/home, and have a link to about page, which is hardlinked to http://example.com/about. This is accessible within 'home.php' (or wherever /home is hosted) by relatively linking About. This follows standard directory protocols and won't change much. Plenty of these examples are documented in the link I provided for more information.
Edit: Initial question was too vague for my answer to be true to what the question really calls for. Proper answer, how to replace, is below.
Use str_replace(), as indicated in the comment below. An example, for how to convert "/ihome". Suppose we are at location http://example.com/about.
$body = ...; // assuming this is going through whole php document
$url = "http://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";
$tokens = explode('/', $url);
array_pop($tokens); // remove "/about"
$url = implode("", $tokens);
str_replace("/ihome", $url . "/ihome", $body);
The last line is certainly up for debate, but this should work. If you have multiple links you want to check, you may want to go with a regex approach (ie any links defined as just "/sub" or any queries "?query").
Theres probably a way to do this with a single preg_replace function in each loop, but something like this should do the trick.
<?php
foreach($urls as $key => $val)
if(!preg_match('/^http\:\/\/www\.example\.com\//', $val))
$url[$key] = 'http://www.example.com/'.ltrim($val, '/');

Is it possible to get information from this type of url: website.com/hello/richard

For example, if there is a url like www.website.com/hello/richard, would it be possible to echo hello and 100 separately onto my page.
eg:
hello how are you today richard
You can get the data from $_SERVER['REQUEST_URI'] and then do whatever you like with it.
Yes it would be. Try this:
$myURL = $_SERVER['REQUEST_URI'];
$myTokens = explode('/', $myURL);
echo $myTokens[1] . "blah" . $myTokens[2];
This code gets the current URL into the myURL variable, then it calls a function called explode which turns it into an array based on the position of the '\' character. Then it echos out certain elements of that array. If you play around with output using echo you will soon see for yourself what is going on.
Sure that's possible. You can get URL as a string using $_SERVER['request_uri]. Then you might want to use explode function to firm array of strings where delimiter is /. Then you may parse it. Or you can do this via .htaccess using rewrite rule

Categories