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.
Related
This question already has answers here:
Remove portion of a string after a certain character
(15 answers)
Closed 2 years ago.
I know I can do this like following way, but is there a one line method like using regex to get everything before the comma as $first and everything after comma as $second?
$data = "m-12,s-52";
$arr = explode(",", $data);
$first = $arr[0];
$second = $arr[1];
You can get the $first and $second using this one line regex code
$data = "m-12,s-52,s-52";
$beforefirstcomma = preg_replace("/,(.+)/", "", $data);
$afterlastcomma = preg_replace("/(.+),/", "", $data);
$afterfirstcomma = preg_replace("/^[^,]+,+/", "", $data);
// outputs
echo $beforefirstcomma; // m-12
echo "<br>";
echo $afterlastcomma; // s-52
echo "<br>";
echo $afterfirstcomma; // s-52,s-52
Final Note
I still recommend using explode() because it is more efficient and performant than running preg_replace() multiple times on the $data string.
This question already has answers here:
How can I get parameters from a URL string?
(12 answers)
Closed 7 years ago.
How can i get all the $_GET name => values from a url using preg_match_all
Example url1: http://www.example.com?go1=test1
Example url2: http://www.example.com?go2=test2
Example url3: http://www.example.com?go1=test1&go3=test3
The return need to be via array if is possible
$array = array();
$array[name1] = value1;
$array[name2] = value2;
...
How about something like:
$url = 'http://www.example.com?go1=test1&go3=test3';
$query = parse_url($url, PHP_URL_QUERY);
parse_str($query, $array);
This will put the query string parameters in $array.
try to look $_SERVER['QUERY_STRING'] option,hopefully it will be helpful.
This may help you to get variables from url, you can also use parse_url to get a single Url components like
Using parse_url:
$url='http://www.example.com?go1=test1&go3=test3';
var_dump(parse_url($url)['query']);
See Demo :http://codepad.viper-7.com/AVFDdy
Using preg_match_all:
<?php
$re = "/\\?(.*)/";
$str = "http://www.example.com?go1=test1";
preg_match_all($re, $str, $matches);
echo '<pre>';
print_r($matches);
echo '</pre>';
See Regex :https://regex101.com/r/eB0rQ2/1
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:
Closed 10 years ago.
Possible Duplicate:
PHP Get end string on url between / and /
I have this code to get the last part of the url between / and / at the end.
The url is:
http://mydomain.com/category/mobile/
I need to retrieve "mobile" from it.
The code i'm using is:
$url = trim($url, '/');
//Put it in a variable
$the_category = substr($url, strrpos($url, '/'));
//whats in it?
echo $the_category;
The above returns nothing ... any ideas?
You should explode an array using "/" as the delimiter and then get the last value of the array, like so:
<?
$url = $_SERVER['REQUEST_URI'];
$url = trim($url, '/');
$array = explode('/',$url);
$the_category = end($array);
//what's in it?
echo $the_category;
?>
How about:
$the_category = basename($url);
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