GameDayRoi GameDayRoi - 10 months ago 28
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.
Google Play Url:

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


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 = "";
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 = "";

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