This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Extracting the last segment on an URI
I want to get the last parameter of the url for eg.
http://www.youtube.com/embed/ADU0QnQ4eDs
I have a full url like above and i want to get only last parameter with the PHP which is
ADU0QnQ4eDs
Please help
Something like this?
echo end(explode("/", $url));
This will throw an error if strict error reporting is enabled.
To avoid this, split them up like this:
$parts = explode("/", $url);
echo end($parts);
There are functions for that.
$url = 'http://www.youtube.com/embed/ADU0QnQ4eDs';
$url_path = parse_url($url, PHP_URL_PATH);
$basename = pathinfo($url_path, PATHINFO_BASENAME);
// $basename is "ADU0QnQ4eDs"
See http://php.net/parse_url and http://php.net/pathinfo.
You have a pattern here that can be easily scanned:
$interestedIn = sscanf($url, 'http://www.youtube.com/embed/%s');
Don't make your live more complicated than it needs to be. Technically substr would work, too, but this one adds more context.
Related
This question already has answers here:
Parsing URL query in PHP [duplicate]
(5 answers)
Closed 3 years ago.
I am trying to get the file name from URL and here I use basename to get the file name but it not works.
basename('https://test-assets.amazonaws.com/live%2Ftestsamantha-9-bucket%2Ff132eb33-f403-4881-80e2-de1cc3bdb43a.jpg');
Result is
live%2Ftestsamantha-9-bucket%2Ff132eb33-f403-4881-80e2-de1cc3bdb43a.jpg
How do I get
f132eb33-f403-4881-80e2-de1cc3bdb43a.jpg
You take the url just as you did, use urldecode on it, so slashes are plain slashes and nothing encoded. Then all you have to do, is explode by slash and take the last element of the created array. Voila.
$url = urldecode(basename('https://test-assets.amazonaws.com/live%2Ftestsamantha-9-bucket%2Ff132eb33-f403-4881-80e2-de1cc3bdb43a.jpg'));
$parts = explode('/', $url);
print end($parts); // Prints 'f132eb33-f403-4881-80e2-de1cc3bdb43a.jpg'
Or better, do it like the other poster did, its less code and he is right. While my solution works too, its more than needed.
You need to decode the url first using the urldecode method:
basename(urldecode('https://test-assets.amazonaws.com/live%2Ftestsamantha-9-bucket%2Ff132eb33-f403-4881-80e2-de1cc3bdb43a.jpg'));
The urldecode method converts the url to https://test-assets.amazonaws.com/live/testsamantha-9-bucket/f132eb33-f403-4881-80e2-de1cc3bdb43a.jpg and then the basename method works properly.
This question already has answers here:
Strip off specific parameter from URL's querystring
(22 answers)
Closed 4 years ago.
if I have some URL like these:
http://localhost/myfile.php?start=2018&end=2019&page=1&status=
Did anynone know how to replace any words from that link that contain page= ?
The result that I want is :
http://localhost/myfile.php?start=2018&end=2019&status=
Even if the link is like this:
http://localhost/myfile.php?start=2018&end=2019&status=&page=3
I still want the result will be without page variable and value, so it might be:
http://localhost/myfile.php?start=2018&end=2019&status=
any idea how to do this? Especially without regex (regex is my last option)
You can use parse_url(), parse_str() and http_build_query():
// Your URL
$str = 'http://localhost/myfile.php?start=2018&end=2019&page=1&status=';
// Get parts
$parts = parse_url($str);
// Get array of arguments
parse_str($parts['query'], $args);
// Remove unwanted index
unset($args['page']);
// Rebuild your URL
echo $parts['scheme'] . '://' . $parts['host'] . $parts['path'] . '?' . http_build_query($args);
// Output: http://localhost/myfile.php?start=2018&end=2019&status=
I encouraged to read documentation or to print_r() vars of this sample code for a better understanding.
This question already has answers here:
URL rewriting with PHP
(5 answers)
Closed 5 years ago.
I am unsure if it's possible to simply do some replace inside php or if i should look at .htaccess for this? What would be the best solution to clean up this url as desired.
The reason why this is not a duplicate is because i dont have a static url, the id's and text will be changed depending on what post is clicked on the website.
i have tried to do this with .htaccess but it doesn't work for me since i fetch different titles and id's depending on what post users decide to watch. this is how my href looks like
<?php echo $row['postTitle'] ?>
I would like to know how i would change this url for example
www.website.com/index.php?page=viewpost&id=26&title=title%of%the%post
to
www.website.com/index.php?page=viewpost/26/title_of_the_post
you didn't really explain what you are trying to do but here's how to exactly what you ask for.
<?php
$url = "www.website.com/index.php?page=viewpost&id=26&title=title%of%the%post";
$parts = explode('?',$url);
$url = $parts[0] . '?page=' $parts[1] ;
unset($parts[0]);
unset($parts[1]);
foreach($parts as $key => $part) {
$url .= '/'.$part;
}
echo $url;
?>
This question already has answers here:
parse youtube video id using preg_match [duplicate]
(10 answers)
Closed 9 years ago.
So I've got this RSS file that I'm trying to get part of a URL from. So here's what I tried (which is not working).
I've got this URL I can get easily enough:
http://www.youtube.com/watch?v=tUPjxGmh9i8&feature=youtube_gdata
I tried doing an $link = ltrim($link, 'http://www.youtube.com/watch?v='); in PHP and an $link = rtrim($trim, '&feature=youtube_gdata');
And it returned "UPjxGmh9i8". It cuts off the "t" in the front. The PHP.net documentation pages aren't the easiest for me to read and interpret, but I'm assuming that any individual character within the second parameter of ltrim() and rtrim() is taken out and this is not what I want. Is there some other solution I can use to grab only the text I want?
Any help is greatly appreciated.
This is how I would do it...
$query = parse_url(
'http://www.youtube.com/watch?v=tUPjxGmh9i8&feature=youtube_gdata',
PHP_URL_QUERY);
parse_str($query, $params);
$slug = get_magic_quotes_gpc() ? stripslashes($params['v']) : $params['v'];
CodePad.
The reason trim() (or its variants) won't work is because it accepts a character list (ordinal not significant), of which http://www.youtube.com/watch?v= contains t. It does not accept a string to remove.
Using PHP, cant you just grab the value of v?
$result = $_GET['v'];
var_dump($result);
This question already exists:
Closed 11 years ago.
Possible Duplicate:
How to remove parts in a URL?
I want to know how to remove first two parts of a URL in php?
I have a url called - http://localhost/photo/user-content/uploads/wall/o/74/ruveena.jpg
It means i want to remove - http://localhost/photo/
and I want make this URL to
user-content/uploads/wall/o/74/ruveena.jpg
I searched evrywhere but there only how to remove query part or host n those stuff i want to remove just only the first two parts of this url.
$url = parse_url('http://localhost/photo/user-content/uploads/wall/o/74/ruveena.jpg', PHP_URL_PATH);
$url = trim($url, '/');
$new_path = substr($url, strpos($url, '/') + 1);