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'];
Related
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:
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]
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);
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Detecting a url using preg_match? without in the string
I wanting to search a string that is input by the user, for instances of http:// if one is found I want to wrap the link in the correct html,
so for example, the following could be input by a user,
Google's url is http://www.google.com,
what I want to save that as it,
Google's url is http://www.google.com
is this possible?
function make_clickable($sText) {
$sClickText = str_replace(' www.', ' http://www.', $sText);
$sClickText = preg_replace("/([\s])?(http|ftp|https)([\:\/\/])([^\s]+)/i", " $2$3$4",$sClickText);
return $sClickText;
}
Tadaa!