How can I grab just the text I want - PHP [duplicate] - php

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);

Related

Get portion of URL with PHP [duplicate]

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];
}

How to extract file name from AWS url? [duplicate]

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.

Extract site link from google URL [duplicate]

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'];

How to extract last part in PHP? [duplicate]

This question already has answers here:
How do I extract query parameters from a URL string in PHP?
(3 answers)
Closed 8 years ago.
This would be an example:
redirect
dynamic_word can be changed because it is dynamic. When click "redirect", dynamic_word will be extracted. So, how to extract it in redirect.php file ? Thanks !
Use $_GET to get parameters from an URL
<?php
$thatName = $_GET['q'];
echo $thatName;
Result
dynamic_word
If samitha's correct looking answer is incorrect then perhaps you mean you would like to extract the dynamic word from a string.
In that case you could do
<?php
$string = 'http://mywebsite.com/redirect.php&q=dynamic_word';
$ex_stirng = explode('&q=', $string);
$dynamic_word = $ex_string(1);
?>
Or even use the strstr function:
http://www.php.net/manual/en/function.strstr.php

PHP Regular expression - forget everything after? [duplicate]

This question already has answers here:
Beautiful way to remove GET-variables with PHP?
(12 answers)
How to remove content from url after question mark. preg_match or preg_replace?
(2 answers)
Closed 8 years ago.
I have this url: http:www.blabla.com/x/x/x/x?username=testuser
I need a string to read this url, but forget everything and including the ? mark.
So it becomes this: http:www.blabla.com/x/x/x/x
The reason for this is because I am making this variable:
$host = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
And this code:
if($host == "http:www.blabla.com/x/x/x/x") {
echo "lul";
}
But right now, the URL changes depending on what user is on, and it has to execute the echo no matter what user is on.
So I read some reges and preg_match etc. and I just wanted to hear your opinions or advice. How would I accomblish this the best? thanks!
This is too trivial of a task for regex.
$host = $_SERVER['SERVER_NAME'] . explode("?", $_SERVER['REQUEST_URI'], 2)[0];
(Note: this assumes you're up-to-date, or at least using PHP 5.4, for the dereference to work without a temporary variable)
Or if you must omit the get / request section just explode ? and use $host[0]

Categories