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.
Related
This question already has answers here:
Get parts of URL in PHP
(4 answers)
Closed 1 year ago.
I had previously been using basename to grab the last part of my URL however have noticed some issues if my URL contains parameters.
So if my URL is this:
https://www.google.com/test-page/?utm_source=test
How would I pull test-page from this?
You split it by the / delimiter, and then take the fourth item
$link = 'https://www.google.com/test-page/?utm_source=test';
$split = explode('/', $link);
if(isset($split[3]))
{
echo $split[3];
}
This question already has answers here:
RegEx to parse or validate Base64 data
(10 answers)
Closed 6 years ago.
i have this case
$var = 'http://example.com/images/image_1.jpg';
$var_2 = 'data:image/png;base64,BASE64_DATA_HERE';
$var_3 = 'data:image/gif;base64,BASE64_DATA_HERE';
how can i make if condition to check if it is base64 or url, i want to check if it base64 first because if i just checked if it is url else it will be base64 it will be very bad, and take care of this "http://example.com/images/image_1.jpg?var=base64" i want to regex check the first of the var not the whole of it
BASE64_DATA_HERE is just example of the big str of base64 code
and i want a preg_match regex code to extract just the BASE64_DATA_HERE
something like
$var = 'data:image/png;base64,ZXhhbXBsZQ==';
preg_match(regex_here,$var,$matches);
$code = $matches[1];
I think you are missing something good out here for server side validations. Just check it out.
Valitron
Anyway for checking base64 string the regex is like this.
^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$
Hope it helps !
This question already has answers here:
How can I get parameters from a URL string?
(12 answers)
Closed 8 years ago.
I want to extract site link from Google URL, I need an efficient way to do this,
I have extracted this, but i am not comfortable with that like,
$googleURL = "http://www.google.ca/local_url?dq=food+Toronto,+ON&q=https://plus.google.com/110334461338830338847/about%3Fgl%3DCA%26hl%3Den-CA&ved=0CHAQlQU&sa=X&ei=HzrCVNX-JqSzigb-94D4CQ&s=ANYYN7nQx_FiR1PuowDmXBi1oyfkI2MImg";
I want this
https://plus.google.com/110334461338830338847/
I have done this in a following way.
$first = current(explode("about", $googleURL)); // returns http://www.google.ca/local_url?dq=food+Toronto,+ON&q=https://plus.google.com/110334461338830338847/
and then,
$myLink = explode("&q=", $first);
echo $myLink[1]; // return my need => https://plus.google.com/110334461338830338847/
but there may be two "about" or "&q=" in a googleURL which can cause problem.
I know that, this googleURL will be redirected to my need, but I need that specific link for a purpose.
I guess that it is not really safe to parse that since google can change its implementation anytime.
However, if you want to get a parameter from a String url, this question covers it pretty well :
How to get parameters from a URL string?
$parts = parse_url($googleUrl);
parse_str($parts['query'], $query);
echo $query['q'];
This question already has answers here:
get json using php
(3 answers)
Closed 9 years ago.
When you're visiting this page:
https://www.googleapis.com/plus/v1/activities/z12gtjhq3qn2xxl2o224exwiqruvtda0i?key=AIzaSyAwnz3yjIcvsosfbudkzl9oogGrT21m6Ns
...I guess what is called an object, appears with a lot of information. But when I try to do a simple GET call and then print it, like so:
$tweets = $_GET['https://www.googleapis.com/plus/v1/activities/z12gtjhq3qn2xxl2o224exwiqruvtda0i?key=AIzaSyAwnz3yjIcvsosfbudkzl9oogGrT21m6Ns'];
print_r($tweets);
...It returns nothing... Why is that?
The $_GET super global is populated with the query string of the current request, it's not used to get stuff from the interwebs.
You're looking for file_get_contents():
$url = 'https://www.googleapis.com/plus/v1/activities/z12gtjhq3qn2xxl2o224exwiqruvtda0i?key=AIzaSyAwnz3yjIcvsosfbudkzl9oogGrT21m6Ns';
$tweets = file_get_contents($url);
print_r($tweets);
If that contains a JSON encoded response you need to additionally use json_decode() to use it.
Do you know how to use $_GET?
Check PHP documentation, you must have made a mistake, or you're using it the wrong way.
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);