PHP preg_match, Finding a package name from Android URL address - php

I need to get Android package name from the URL address.
Here is what I have done.
$url = 'https://play.google.com/store/apps/details?id=com.gamevil.projectn.global&feature=featured-apps#?t=W251bGwsMSwyLDIwMywiY29tLmdhbWV2aWwucHJvamVjdG4uZ2xvYmFsIl0.';
preg_match("~id=(\d+)~", $url, $matches);
$package_name = $matches[1];
echo $package_name;
Package name should be "com.gamevil.projectn.global"
However, my code is not working.
Is there something that I miss?

you can do this by parse_url function
<?php
$url = 'https://play.google.com/store/apps/details?id=com.gamevil.projectn.global&feature=featured-apps#?t=W251bGwsMSwyLDIwMywiY29tLmdhbWV2aWwucHJvamVjdG4uZ2xvYmFsIl0.';
$arr =parse_url($url);
$new = explode("&",$arr['query']);
$new1 = explode("=",$new[0]);
echo($new1[1] );
output
com.gamevil.projectn.global

Maybe this can help you:
$url = 'https://play.google.com/store/apps/details?id=com.gamevil.projectn.global&feature=featured-apps#?t=W251bGwsMSwyLDIwMywiY29tLmdhbWV2aWwucHJvamVjdG4uZ2xvYmFsIl0.';
preg_match("/id=(.*?)&/", $url, $matches);
$package_name = $matches[1];
echo $package_name;
preg_match will no find everything between id= and &.
But a better solution is to use parse_url to parse the url and this function will return the components of the url.

Related

Php parse url and remove parametr issue

I have url like this :
/leads/set_attributes.html?lead_id=3793&name=TEST
I need remove 'name' and return url.
I try do it like this:
$vars = [];
parse_str(html_entity_decode($sAddQuery), $vars);
unset($vars['name']);
$sAddQuery = http_build_query($vars);
But i always get
%3Flead_id=3793
as result. I tried use html_entity_decode for array elements but it did not help. How can i solve this? Thanks
Modify your code to :
<?php
$url = parse_url('/leads/set_attributes.html?lead_id=3793&name=TEST');
$str = $url['query'];
parse_str($str, $params); //parse URL
unset($params['name']); //unset name parameter
$string = http_build_query($params); //again built the URL string
var_dump($string);
?>
Hope it works!

How to get ID from Instagram URL using preg_match

i need to know how to preg_match ID from instagram URL
https://www.instagram.com/p/BrODgnXHlE6/?utm_source=ig_share_sheet&igshid=znsinsart176
i tried
preg_match('/https://www.instagram.com/p/(.)/(.)/U', $video_url, $matches);
$video_id = $matches[1];
But it is not working
Thank you
<?php
$url = "https://www.instagram.com/p/BrODgnXHlE6/?utm_source=ig_share_sheet&igshid=znsinsart176";
preg_match('/https:\/\/www.instagram.com\/p\/(.+)\/(.+)/U', $url, $matches);
$id = $matches[1];

Extract particular point of URL in PHP

I'm trying to get a very specific part of a URL using PHP so that I can use it as a variable later on.
The URL I have is:
https://forums.mydomain.com/index.php?/clubs/11-Default-Club
The particular part I am trying to extract is the 11 part between the /clubs/ and -Default-Club bits.
I was wondering what the best way to do this was. I've seen examples on here that use a regex-esque parser but I can't wrap my head around it for this particular instance.
Thanks
Edit; this is what I've tried so far using an explode query, but it seems to give me all sorts of elements which are not present in the URL above:
$url = $_SERVER['REQUEST_URI'];
$url = explode('/', $url);
$url = array_filter($url);
$url = array_merge($url, array());
Which returns:
Array ( [0] => index.php?app=core&module=system&controller=widgets&do=getBlock&blockID=plugin_9_bimBlankWidget_dqtr03ssz&pageApp=core&pageModule=clubs&pageController=view&pageArea=header&orientation=horizontal&csrfKey=8e19769b95c733b05439755827a98ac8 )
If you expect that the string with dashes (11-Default-Club) will be always at the end you can try this:
$url = $_SERVER['REQUEST_URI'];
$urlParts = explode('/', $url);
$string = end($urlParts);
$stringParts = explode('-', $string);
$theNumber = $stringParts[0]; // this will be 11
I'd rather be explicit:
<?php
$url = 'https://forums.mydomain.com/index.php?/clubs/11-Default-Club';
$query = parse_url($url, PHP_URL_QUERY);
$pattern = '#^/clubs/(\d+)[a-zA-Z-]+$#';
$digits = preg_match($pattern, $query, $matches)
? $matches[1]
: null;
var_dump($digits);
Output:
string(2) "11"
If this URL structure is fix for all URLs in your site and you only want to get the integer/number/digit part of the URL:
<?php
$url = 'https://forums.mydomain.com/index.php?/clubs/11-Default-Club';
$int = (int) filter_var($url, FILTER_SANITIZE_NUMBER_INT);
echo $int;
If this url structure is fix for all URLs in your site then below is best way to get your value.
<?php
$url = "https://forums.mydomain.com/index.php?/clubs/11-Default-Club";
$url = explode('/', $url);
$url = array_filter($url);
$end = end($url);
$end_parts = explode('-',$end);
echo $end_parts[0];
Output:
11

PHP: Get parameter from encoded url

I have this url:
/?goto=%2Fr%2Faccount%2Findex%2Ecfm%3Fsite_id%3D87211
How can I GET site_id from this url?
Check this way,
<?php
$abc ="/?goto=%2Fr%2Faccount%2Findex%2Ecfm%3Fsite_id%3D87211";
$test = parse_url($abc);
$test = urldecode($test["query"]);
$url = parse_url($test);
parse_str($url['query'], $param);
print_r($param["site_id"]);
?>
Check your output here :https://eval.in/621360
Try parse_str and parse_url on $_GET['goto'].
parse_str(parse_url($_GET['goto'], PHP_URL_QUERY), $params);
$site_id = $params['site_id'];
If you know your url's will always be in this structure, you can parse it out with RegEx like so:
$url = '/?goto=%2Fr%2Faccount%2Findex%2Ecfm%3Fsite_id%3D87211';
preg_match('/site_id\%3D(\d+)/', $url, $matches);
$siteId = $matches[1];
It's searching for anything that matches site_id%3D and the numeric value that follows that.
Try using rawurldecode in URL function
by using $_GET['goto'] you will get following url :
/r/account/index.cfm?site_id=87211
Try this
$uri = $_GET['goto'];
$site = explode("=", $uri);
echo $site[1];

How to trim down a URL using regex in PHP?

I am struggling to finish this regex code in PHP. I want to trim down the following url which is held in variable $text so that it goes from:
http://www.site.net/showthread.php?tid=324&pid=...
to:
showthread.php?tid=324
Thank you kindly!
Why use a regex? The parse_url method should give you all you want: http://php.net/manual/en/function.parse-url.php
Edit: working example
$someurl = 'http://www.site.net/showthread.php?tid=324&pid=...';
$urlParts = parse_url($someurl, PHP_URL_PATH | PHP_URL_QUERY);
$params = parse_str($urlParts['query']);
unset($params['pid']);
$queryString = http_build_query($params);
$newUrl = $urlParts['path'] . '?' . $queryString;
Since $urlParts['path'] start with a / and you didn't want that, you could even use
$newUrl = substr($newUrl, 1);
and be done :) Does that help at all?
This should do it:
$url = 'http://www.site.net/showthread.php?tid=324&pid=...';
$pattern = "/showthread.php\?tid=[0-9]+/";
if (preg_match($pattern, $url, $match))
print_r($match);

Categories