How to get URL last part with php - php

I was searching for solutions to display the current URL of the page, and I found a few ones but I don't know how to implement them and call them, so this was the best solution I've found for me because it already has the echo thingy.
function curPageURL() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
And to call it, I'm calling it like this
echo curPageURL();
But I want to get only the last part of the URL, for exemple:
http://stackoverflow.com/posts/29237151/thequestion
I want to get thequestion part of the URL. how can I do this?

As stated in the comments, the best way is to explode() then array_pop() your URL.
Like so:
function curPageURL() {
$url = $_SERVER['REQUEST_URI'];
$url = explode('/', $url);
$lastPart = array_pop($url);
return $lastPart;
}
#Vineet answer is suitable too.

In your case. Please change like
$url = curPageURL();
It will give you complete URL and then write lines as below
$new = explode("/", $url);
$last_part = end($new);
It will give your desired output.

You could try this as well
echo substr(strrchr(curPageURL(), "/"), 1);

http://php.net/manual/en/function.parse-url.php
PHP has a function called parse_url() that will ... parse an URL.
What you are looking for is the path part of the result.
<?php
$url = 'http://stackoverflow.com/posts/29237151/thequestion?arg=value#anchor';
$pathParts = parse_url($url, PHP_URL_PATH);
$lastPart = array_pop(explode('/', $pathParts));
echo $lastPart;

The shortest way I think is:
end(explode('/', $url));

Related

How to get specific part of url?

I am working on search function which I want make my search more easier and I had store href inside my db. Therefore, I need to get specific part of current page url eg : abc.php.
But now I only can get full url which is eg : http://abc_system/user/abc.php. Is it one of the solution is used substring?I am looking for some help. Hope you guys can help me out. Thanks in advanced.
This is my code which return url result:
function curPageURL() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
You need to use basename() function for get your filename from the URL string.
<?php
$url = "http://google.com/sdfsaf/abcd.php";
echo basename($url); // It will returns abcd.php
?>
Demo
Use **parse_url()** if you want to split url to its components,
For more info, Refer : http://www.php.net/manual/en/function.parse-url.php

PHP posting to twitter in bitly url format

I've managed to put together the following script:
<?php
/* make a URL small */
function make_bitly_url($url,$login,$appkey,$format = 'xml',$version = '2.0.1')
{
//create the URL
$bitly = 'http://api.bit.ly/shorten?version='.$version.'&longUrl='.urlencode($url).'&login='.$login.'&apiKey='.$appkey.'&format='.$format;
//get the url
//could also use cURL here
$response = file_get_contents($bitly);
//parse depending on desired format
if(strtolower($format) == 'json')
{
$json = #json_decode($response,true);
return $json['results'][$url]['shortUrl'];
}
else //xml
{
$xml = simplexml_load_string($response);
return 'http://bit.ly/'.$xml->results->nodeKeyVal->hash;
}
}
//function to get the url of the event!
function curPageURL() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
/* usage */
$short = make_bitly_url('http://site.com/viewEvent.php?id=2323232','bitlyuser','bitlyapikey','json');
echo 'The short URL is: '.$short . "<br>";
echo "PATH: ". curPageURL();
// returns: http://bit.ly/11Owun
?>
Now this code can produce the a short url of whatever is passed to it. I have a tweet button on my site that I got from twitter developer site. it works in that it posts the fully link of the page it is currently on...so not the shorten version. Now i want when that twitter button is pressed for it produce a short url so that I could share on my site's account. How is that done?
Thank you,
You should be able to just set the data-url option to the bitly url. e.g.
Tweet

How to get value from url?

If I have url like this: www.example.com/product/yellow-bed is it possible to retrieve product name from url?
For example, if url would be like www.example.com?page=product&product_name=yellow_bed I would use:
$product = $_GET['page'];
$product_name = $_GET['product_name'];
But how to get it from www.example.com/product/yellow-bed ?
Get the URI by "$_SERVER["REQUEST_URI"]". Convert the string into an array with explode:
$uri = 'www.google.com/product/yellow-bed' //uri = $_SERVER["REQUEST_URI"]
$uriArray = explode('/', $uri);
$product = $urlArray[1];
$product_name = $urlArray[2];
0 = www.google.com, 1 = product, 2 = yellow-bed
PHP manual: array explode ( string $delimiter , string $string [, int $limit ] ).
Some php frameworks (like CodeIgniter) has already this function implemented.
Never the less you can have a look here: http://erunways.com/simple-php-get-uri-or-segment-element/ , and that should solve your problem.
You could fetch the complete URI with $_SERVER['QUERY_STRING'] and then split it apart. Or you work with an .htaccess and internally rewrite the url.
Try $_SERVER[ 'PATH_INFO' ] or $_SERVER[ 'REQUEST_URI' ].
You should have a look at $_SERVER['REQUEST_URI']. This will be /product/yellow-bed in your example.
You can then use strrpos(), explode() orpreg_match()` (or other string manipulation functions) to extract what you want.
you can use the following function to retrive current url:
<?php
function curPageURL() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
?>
now you can take url and parse its elements using parse_url : http://php.net/manual/en/function.parse-url.php

How do I pull information from URL in PHP?

Lets say the url is http://example.com/product/3 and I only want to retrieve what is after http://example.com/product/. I found this, it echos the domain but how do I get the three. Its this method reliable? I'm using codeIgniter also.
<?php
function curPageURL() {
$pageURL = 'http';
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
?>
<?php
echo curPageURL();
?>
Use $this->uri->segment(n). Documentation for it is here:
http://codeigniter.com/user_guide/libraries/uri.html
For your code, you would use:
$curPageURL = $this->url->segment(2)
Edited: Fixed a bug in the code.
Yes, use the URI class of the codeigniter API.
The base_url is segment zero, so in your case, products would be segment 1 and the id segment 2.
$product_id = $this->uri->segment(2)
As chetan mentioned this is clearly documented in the user guide.
You should use parse_url instead:
http://php.net/manual/es/function.parse-url.php

How to strip a GET property from the URL using PHP

I have the following function that get's the current page URL:
<?php
// get current page url
function currentPageUrl() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
}
else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
echo $pageURL;
}
?>
Which prints:
http://localhost/gallery.php?id=23&type=main
I want to remove "&type=main" which is present in the url. So before echoing $pageURL I add the following line:
$pageUrl = preg_replace("&type=main", "", $pageURL);
But it still returns the full url including type=main. How can I get rid of that from the url?
Another solution could be to :
use parse_url or $_SERVER['QUERY_STRING'] to extract the list of parameters as a string
use parse_str to transform the query string to an array containing each parameter and its value -- indexed by parameters names.
Do some magic on that array :
do what you have to to filter it
For example, unset($array['type']); could probably help ;-)
If needed, add more parameters to that array
And, then, use http_build_query to re-build a query-string.
A bit more complex than string manipulations, of course -- but much more reliable, I'd say ;-)
You can throw a url into parse_url. It will return an array from which you can rebuild as you see fit.
Try this:
$pageUrl = str_replace('&type=main', '', $pageURL);
did you try any other $_SERVER variables?
there are plenty and some of them already contain everything you need without any replace
phpinfo(32);
will show you all
PHP identifiers are case sensitive. You probably meant to assign it to the same variable.
$pageURL = preg_replace("&type=main", "", $pageURL);
Either that, or you need to change the remnant of code to use $pageUrl instead of $pageURL.

Categories