-1

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.

Irfan Ahmed
  • 9,136
  • 8
  • 33
  • 54
  • 1
    I doubt you will have 2 `&q=`. The second one would override the first one. Why not use a regex like `preg_match("/https:\/\/plus.google.com\/[0-9]+\//", $googleURL, $match);` – putvande Jan 23 '15 at 13:33
  • what if i have another "about" instead of "food" like http://www.google.ca/local_url?dq=about+ – Irfan Ahmed Jan 23 '15 at 13:35
  • Parse the url, then the query string and then the url in the query string again if needed: http://php.net/manual/en/function.parse-url.php – jeroen Jan 23 '15 at 13:36

1 Answers1

3

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'];
Community
  • 1
  • 1
Johnride
  • 8,476
  • 5
  • 29
  • 39