How to get a part of url from php - 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!

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

Use $_GET['id'] to get the query string value of id
<?php
echo $_GET['id'];
?>

You could do this with regex: (?:\?id=)(.*)\b (I'm sure there's a more effective regex for this, but this accomplishes what you require)
preg_match('/(?:\?id=)(.*)\b/', 'https://play.google.com/store/apps/details?id=com.zing.zalo&hl=en', $matches);
print_r($matches);
Returns:
Array
(
[0] => ?id=com.zing.zalo&
[1] => com.zing.zalo
)

Related

Using preg_replace on url variables

I have some very long URL variables. Here is one example.
http://localhost/index.php?image=XYZ_1555025022.jpg&mppdf=yes&pdfname=Printer&deskew=yes&autocrop=yes&print=no&mode=color&printscalewidth100=&printscaleheight100=&rand=56039
Ultimately it would be nice if I could find a way to use preg_replace to simply change one variable even if in the middle of the string for instance in the string above change print=no to 'print=yes for example.
I will however settle for a preg_replace pattern match that allows me to delete ?image=XYZ_1555025022.jpg. as this is a variable the name could be anything. It will always have "?image" " at the start and end with "&"
I think one of the problems I have run into is that preg_match seems to have issues on strings with "=" contained in them .
I am completely lost here in this and all those characters make may head spin. Maybe someone can give some guidance please?
Here's a demo of how you can do some of things you want using explode, parse_str and http_build_query:
$url = 'http://localhost/index.php?image=XYZ_1555025022.jpg&mppdf=yes&pdfname=Printer&deskew=yes&autocrop=yes&print=no&mode=color&printscalewidth100=&printscaleheight100=&rand=56039';
// split on first ?
list($path, $query_string) = explode('?', $url, 2);
// parse the query string
parse_str($query_string, $params);
// delete image param
unset($params['image']);
// change the print param
$params['print'] = 'yes';
// rebuild the query
$query_string = http_build_query($params);
// reassemble the URL
$url = $path . '?' . $query_string;
echo $url;
Output:
http://localhost/index.php?mppdf=yes&pdfname=Printer&deskew=yes&autocrop=yes&print=yes&mode=color&printscalewidth100=&printscaleheight100=&rand=56039
Demo on 3v4l.org
You can use str_replace() or preg_replace() to get your job done, but parse_url() with parse_str() will give you more controls to modify any parameters easily by array index. Finally use http_build_query() to make your final url after modification.
<?php
$url = 'http://localhost/index.php?image=XYZ_1555025022.jpg&mppdf=yes&pdfname=Printer&deskew=yes&autocrop=yes&print=no&mode=color&printscalewidth100=&printscaleheight100=&rand=56039';
$parts = parse_url($url);
parse_str($parts['query'], $query);
echo "BEFORE".PHP_EOL;
print_r($query);
$query['print'] = 'yes';
echo "AFTER".PHP_EOL;
print_r($query);
?>
DEMO: https://3v4l.org/npGij

PHP print only some part of the string

I have few PHP strings ($url1, $url2, ..) as below:
$url1 = "http://build:f9280396f83a0#mobile.com:8080/job/new-ios-2.1/buildWithParameters";
$url2 = "http://build:f9280396f83a0#mobile.com:8080/job/new-android-8.2/buildWithParameters";
How do I get the substring after /job/ and before /buildWithParameters? Here is the expected output for $url1:
new-ios-2.1
So far I have tried using substr function:
For example, $url1 = substr($url, -10);. I am not able to find the above desired job part by this approach. Is there a better way to do this in PHP?
You Can use explode function . 4th key's value will be as per your expectation
$url1 = "http://build:f9280396f83a0#mobile.com:8080/job/new-ios-2.1/buildWithParameters";
$urlarr=explode('/', $url1);
print_r($urlarr);
Use this regex:
preg_match('/\/job\/([^\/]*)\/buildWithParameters/', $url1, $matches);
print_r($matches[1]);
This will match the string between first occurrence of /job/ and first occurence of /buildWithParameters/
Check the demo here

Exploding a URL

What am I doing wrong? I currently have the following code below and I am trying to just get the secondary url which links to the direct MP3 path.
$date = $html2->find('strong > a',0);
$explode = explode ("http://www.example.com/?dl_name=", $date->href);
print $explode;
It returns ARRAY.
explode returns an array of strings, try using print_r($explode) and then use $explode[0], $explode[1]...
Could you resolve this with str_replace:
$secondary_url = str_replace("http://www.example.com/?dl_name=", '', $date->href);
You will get the rest of this string in $secondary_url.
Reference: str_replace

How to get part from URL

Well sorry for the probably misleading title. Wasn't sure how to describe it better.
When accessing the status page I want to get the attached ID. But I don't want to use GET fields (wordpress makes /status?id=2134 to /status/?id=1234 - that's the only reason actually).
So this is my url
http://foo.bar.com/status/1234/
I want to get 1234
Okay fine. I could use something like $_SERVER["REQUEST_URI"] + trim() for example. Probably regex would be the key to get this job done since one could do something like /status/1234/foo/bar/baz/.. But I'm wondering if there is something builtin with PHP to get this part of the url.
Use the parse_url() function, and extract it:
$url = 'http://foo.bar.com/status/1234/';
$path = trim(parse_url($url, PHP_URL_PATH), '/');
$items = explode('/', $path);
$num = array_pop($items);
var_dump($num);
You can also use a regular expression, if that tickles your fancy:
$url = 'http://foo.bar.com/status/1234/';
$path = parse_url($url, PHP_URL_PATH);
preg_match('~/status/(?P<num>\d+)/?~', $path, $result);
$num = isset($result['num']) ? $result['num'] : null;
var_dump($num);
Try to parses a URL and returns an associative array containing any of the various components of the URL that are present using parse_url, explode it using explode and finally select status id using end
Try like this
$url = 'http://foo.bar.com/status/1234/';
$statusId = explode('/',trim(parse_url($url, PHP_URL_PATH), '/'));
print end($statusId);
Demo Ex http://ideone.com/34iDnh
trim- http://php.net/trim
explode-http://php.net/explode
parse_url-[1]: http://php.net/manual/en/function.parse-url.php

Grab a word in a complex url by php

Hi I have this url where i want to grab the last word (oath2_access_token) after the equals sign by PHP where the last word can be anything not just oath2_acc..
https://api.linkedin.com/v1/peoplesearch:(facets:(code,buckets:(code,name,count)))?facets=industry,network&facet=industry,12&facet=network,F&oauth2_access_token=oath2_access_token
Please help to grab the word or atleast provide me the resources where i could learn and do it myself.
Thanks.
You can use explode to get all the values after the equal signs and then just get the last element of the array:
<?php
$url = 'https://api.linkedin.com/v1/peoplesearch:(facets:(code,buckets:(code,name,count)))?facets=industry,network&facet=industry,12&facet=network,F&oauth2_access_token=oath2_access_token';
$array = explode('=', $url);
$value = end(array_values($array));
echo $value;
?>
GET
(As others have pointed out) I'm not sure why you can't simply use this...
$token = $_GET['oauth2_access_token'];
http://php.net/_get
Regex
Seeing as you have tagged this question with regex...
preg_match('/.*=(.*)/', $url, $matches);
$token = $matches[1];
.*= => Select everything up to and including the last = sign (because * is greedy)
(.*) => Select everything after the last = sign and capture it
http://php.net/preg_match
http://www.regular-expressions.info/
Explode
You could also split the url on the = sign and take the last index...
$url_array = explode('=', $url);
$token = end($url_array);
http://php.net/explode
http://php.net/end
preg_match("/oauth2_access_token\=([a-z0-9_\-]+)/i", $url, $matches);
I guess this pattern should cover the token, if not you'll need to define the allowed characters between the [] brackets.
Dump $matches to see which index grabs the token.
Either use
$_GET['oauth2_access_token']
or use parse_url. :
<?php
$url = "https://api.linkedin.com/v1/peoplesearch:(facets:(code,buckets:(code,name,count)))?facets=industry,network&facet=industry,12&facet=network,F&oauth2_access_token=oath2_access_token";
$querystring_params = array();
parse_str(parse_url($url, PHP_URL_QUERY), $querystring_params);
echo $querystring_params["oauth2_access_token"];
?>

Categories