GameDayRoi GameDayRoi - 6 months ago 16
PHP Question

How to get a part of url from php

how can I use php to get exactly the id from google play url.
Example:
Google Play Url: https://play.google.com/store/apps/details?id=com.zing.zalo&hl=en

I want to get the com.zing.zalo . Thank you!

Answer

Simple Way

use preg_match or get the id from the url using $_GET['id'], if you get this from url as other answer did.

$Url = "https://play.google.com/store/apps/details?id=com.zing.zalo&hl=en";
preg_match("/[^?]+(?:\?id=([^&]+).*)?/", "$Url", $matches);

echo $matches[1]; //com.zing.zalo

Working Example here Check online

The Longest way:

Simply you can use some PHP function to get it. Lets you have the following url.

$Url = "https://play.google.com/store/apps/details?id=com.zing.zalo&hl=en";

so what you need to explode the url using ? which is only one on the url.

$arr = explode("?", $Url);

From that array you need to store only the second part cause you need query string. So take only $arr[1]. Now explode again the $arr[1] with the & sign which is divide the rest of the url i mean $arr[1].

$arr2 = explode("&", $arr[1]);

Now you are all set, use another explode function to get the com.zing.zalo from the $arr2[0].

$idval = explode("=", $arr2[0]);

Result, Just echo the second part of the $idval array.

echo $idval[1]; //com.zing.zalo