This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
extract last part of a url
I have a url like this:
http://www.domain.co.uk/product/SportingGoods/Cookware/1/B000YEU9NA/Coleman-Family-Cookset
I want extract just the product name off the end "Coleman-Family-Cookset"
When I use parse_url and print_r I end up with the following:
Array (
[scheme] => http
[host] => www.domain.co.uk
[path] => /product/SportingGoods/Cookware/1/B000YEU9NA/Coleman-Family-Cookset
)
How do I then trim "Coleman-Family-Cookset" off the end?
Thanks in advance
All the answers above works but all use unnecessary arrays and regular expressions, you need a position of last / which you can get with strrpos() and than you can extract string with substr():
substr( $url, 0, strrpos( $url, '/'));
You'll maybe have to add +/- 1 after strrpos()
This is much more effective solution than using preg_* or explode, all work though.
$url = 'http://www.domain.co.uk/product/SportingGoods/Cookware/1/B000YEU9NA/Coleman-Family-Cookset';
$url = explode('/', $url);
$last = array_pop($url);
echo $last;
You have the path variable(from the array as shown above).
Use the following:
$tobestripped=$<the array name>['path']; //<<-the entire path that is
$exploded=explode("/", $tobestripped);
$lastpart=array_pop($exploded);
Hope this helps.
$url = rtrim($url, '/');
preg_match('/([^\/]*)$/', $url, $match);
var_dump($match);
Test
Related
I'm trying to extract id, which is a whole number from a url, for example:
http://example.com/email/verify/106/8be57f01ac84747886acd7ae88c888112135fc7a
I'd like to extract only 106 from the string in PHP. The only dynamic variables in the URL would be the domain and the hash after 106/ The domain, id, and hash after 106/ are all dynamic.
I've tried preg_match_all('!\d+!', $url, $result), but it matches all number in string.
Any tips?
The pattern \b\d+\b might be specific enough here:
$url = "http://example.com/email/verify/106/8be57f01ac84747886acd7ae88c888112135fc7a";
preg_match_all('/\b\d+\b/', $url, $matches);
print_r($matches[0]);
This prints:
Array
(
[0] => 106
)
Try this. This will works.
$url = $_SERVER['REQUEST_URI']; //"http://example.com/email/verify/106/8be57f01ac84747886acd7ae88c888112135fc7a";
$str = explode('/',$url);
echo "<pre>";
print_r($str);
$id = $str[5]; // 106
This question already has answers here:
PHP get the last 3 elements of an associative array while preserving the keys?
(4 answers)
Closed 4 years ago.
How do I get Last 3 parts of the url
For example , I have link like below
http://www.yoursite/one/two/three/drink.pdf
I will get the last part of the url using below code
$url = "http://www.yoursite/one/two/three/drink.pdf";
echo $end = end((explode('/', $url)));
But i need last 3 parts from the url like below
/two/three/drink.pdf
Please provide me a solution.
You could do this:
<?php
$url = "http://www.yoursite/one/two/three/drink.pdf";
$ex = explode('/', $url);
$arr = array_slice($ex, -3, 3);
$output = '/'.implode('/', $arr);
var_dump($output); // Outputs /two/three/drink.pdf
First, you use explode to break the url string into an array. Then use array_slice to get the last three elements of the array and finally implode to glue back the array elements using / as the glue.
Putting it all in one line would look like:
echo '/'.implode('/', array_slice(explode('/', $url), -3, 3));
Try below code
$url = "http://www.yoursite/one/two/three/drink.pdf";
$url_array = (explode('/', $url));
print_r(array_slice($url_array,-3,3));
Hope this helps.
This question already has answers here:
Parsing domain from a URL
(19 answers)
Closed 8 years ago.
http://www.example.com/hu/link
Using PHP how can I only keep the hu? Please note that the link is only one variable, it may be anything
You can use explode
$exploded = explode('/', 'http://www.example.com/hu/link');
$value = $exploded[3];
One solution is this:
$split = explode("/", $_SERVER["REQUEST_URI"]);
echo $split[1];
$url = "http://www.example.com/hu/link";
$split_url = explode('/', $url);
echo $split_url[3];
Output:
hu
You can use a regular expression preg_match like this:
$url = 'http://www.example.com/hu/link';
preg_match('/.*www.*\/(.*)\//i',$url, $matches);
print_r($matches[1]);
This question already has answers here:
PHP Regex to Remove http:// from string
(8 answers)
Closed 9 years ago.
I have a PHP script that removes the "http://" from user input url strings.
My Script:
$url= "http://techcrunch.com/startups/";
$url = str_replace('http://', '', $url);
Result:
$url= techcrunch.com/startups/
This works great, except that sometimes urls have "https://" instead. Is there a way I can just remove everything before the domain name, no matter what it is?
Try this out:
$url = 'http://techcrunch.com/startups/';
$url = str_replace(array('http://', 'https://'), '', $url);
EDIT:
Or, a simple way to always remove the protocol:
$url = 'https://www.google.com/';
$url = preg_replace('#^.+?\:\/\/#', '', $url);
Something like this ought to do:
$url = preg_replace("|^.+?://|", "", $url);
Removes everything up to and including the ://
Use look behinds in preg_replace to remove anything before //.
preg_replace('(^[a-z]+:\/\/)', '', $url);
This will only replace if found in the beginning of the string, and will ignore if found later
preg_replace('/^[^:\/?]+:\/\//','',$url);
some results:
input: http://php.net/preg_replace
output: php.net/preg_replace
input: https://www.php.net/preg_replace
output: www.php.net/preg_replace
input: ftp://www.php.net/preg_replace
output: www.php.net/preg_replace
input: https://php.net/preg_replace?url=http://whatever.com
output: php.net/preg_replace?url=http://whatever.com
input: php.net/preg_replace?url=http://whatever.com
output: php.net/preg_replace?url=http://whatever.com
input: php.net?site=http://whatever.com
output: php.net?site=http://whatever.com
$new_website = substr($str, ($pos = strrpos($str, '//')) !== false ? $pos + 2 : 0);
This would remove everything before the '//'.
EDIT
This one is tested. Using strrpos() instead or strpos().
I have a site and I would need to get one pages URL with PHP. The URL might be something www.mydomain.com/thestringineed/ or it can www.mydomain.com/thestringineed?data=1 or it can be www.mydomain.com/ss/thestringineed
So it's always the last string but I dont want to get anything after ?
parse_url should help you out.
<?php
$url = "http://www.mydomain.com/thestringineed/";
$parts = parse_url($url);
print_r($parts);
?>
You will use the parse_url function, then look at the path portion of the return.
like this:
$url='www.mydomain.com/thestringineed?data=1';
$components=parse_url($url);
//$mystring= end(explode('/',$components['path']));
// I realized after this answer had sat here for about 3 years that there was
//a mistake in the above line
// It would only give the last directory, so if there were extra directories in the path, it would fail. Here's the solution:
$mystring=str_replace( reset(explode('/',$components['path'])),'',$components['path']); //This is to remove the domain from the beginning of the path.
// In my testing, I found that if the scheme (http://, https://, ...) is present, the path does not include
//the domain. (it's available on it's own as ['host']) In that case it's just
// $mystring=$components['path']);
parse_url() is the function you are looking for. The exact part you want, can be received through PHP_URL_PATH
$url = 'http://php.net/manual/en/function.parse-url.php';
echo parse_url($url, PHP_URL_PATH);
use $_SERVER['REQUEST_URI'] it will return full current page url you can split it with '/' and use the last array index . it will be the last string
You can use:
$strings = explode("/", $urlstring);
Which will remove all the '/' in the url and return an array containing all the words.
$strings[count($strings)-1]
Now has the value of the string you need, but it may contain '?data=1' so we need to remove that:
$strings2 = explode("?", $strings[count($strings)-1]);
$strings2[0]
Has the string you are wanting out of the url.
Hope this Helps!
<?php
$url = 'http://username:password#hostname/path?arg=value#anchor';
print_r(parse_url($url));
echo parse_url($url, PHP_URL_PATH);
?>
and your out put is
Array
(
[scheme] => http
[host] => hostname
[user] => username
[pass] => password
[path] => /path
[query] => arg=value
[fragment] => anchor
)
/path