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
Related
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));
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
ok i have a url from $_SERVER['REQUEST_URI']
lets say it give us a url
http://localhost/controller/method
i have tried something like
explode('/',$_SERVER['REQUEST_URI'])
and it gave us like
array
0 => string '' (length=0)
1 => string 'controller' (length=10)
2 => string 'method' (length=6)
what is the best way to get the controller or method ? or removeing the 0 in the array ? ( first array ) ?
so it will be like
$controller = 'controller';
$method = 'method';
from above inputs.
maybe about list ? still no clue using list().
edit heres what ive done so far
$this->url = str_replace(config('foldertoscript'), NULL, $_SERVER['REQUEST_URI']);
$hello = explode('/',$this->url);var_dump($hello);
array_shift($hello);
list($controller,$method) = $hello;
var_dump($hello,$controller);
in a class
Thanks for looking in.
Adam Ramadhan
To remove the first element of an array, you can use array_shift().
$_SERVER['REQUEST_URI'] gives you the url without the "http://www.yoursite.com".
You can use something like this
<?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;
}
echo curPageURL();
?>
Hope this helps.
Use array_shift to remove the first array item.
http://php.net/manual/en/function.array-shift.php
Example:
$your_array = array_shift($your_array);
$controller = $your_array[0];
$method = $your_array[1];
For the same matter I use url_rewriting.
I have a rule that says ^([a-zA-Z0-0-_\/.]+)$ index.php?url=$1
(this is not a copy paste from my code, but you get the idea)
then if you say $_URL = $_REQUEST["url"];
$directive = explode("/",$_URL);
you will get what you need, as for the parameters you could say module/method/id/1/data/2
you have to take care of your parameters and it works if you use the GET method for
navigation only(as it should be used). Also makes the stuff much safer as no one can send SQL
injections via get or any "smart" directives.
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
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.