I used the code below to get "google" printed out
$domain = parse_url('http://google.com', PHP_URL_HOST);
$url = strstr($domain, ".", true);
echo $url;
Works fine as long the url is not like in.google.com (only "in" is printed now).
Is there a workaround?
Note: I need all extensions to be remove .com or .co or .anything
Use regexp instead. Both the following scripts returns "google" inside $matches[1]
with in
$regexp = "/([\w]{0,}).([\w]{2,3}|[\w]{2,3}.[\w]{2,3})$/";
$input = parse_url('http://in.google.com', PHP_URL_HOST);
preg_match($regexp, $input, $matches);
var_dump($matches[1]); // string(6) "google"
without in
$regexp = "/([\w]{0,}).([\w]{2,3}|[\w]{2,3}.[\w]{2,3})$/";
$input = parse_url('http://google.com', PHP_URL_HOST);
preg_match($regexp, $input, $matches);
var_dump($matches[1]); // string(6) "google"
As you can see in the following https://regex101.com's link
https://regex101.com/r/iVyzzw/1/:
it works. Always, in $matches[1], you can find google.
Related
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
String:
https://fakedomain.com/2017/07/01/the-string-i-want-to-get/
Code:
$url = 'https://fakedomain.com/2017/07/01/the-string-i-want-to-get/';
$out = [];
preg_match('\/\d{4}\/\d{2}\/\d{2}(.*)', $url, $out);
// At this point $out is empty...
// Also... I tried this (separately)
$keywords = preg_split("\/\d{4}\/\d{2}\/\d{2}(.*)", $url);
// also $keywords is empty...
I've tested the regex externally and it works. I want to split out the /the-string-i-want-to-get/ string. What am I doing wrong?
I would not use a regex. In this case it's better to use parse_url and some other helpers like trim and explode.
<?php
$url = 'https://fakedomain.com/2017/07/01/the-string-i-want-to-get/';
$parsed = parse_url($url);
$Xploded = explode('/',trim($parsed['path'],'/'));
print $Xploded[count($Xploded)-1];
// outputs: the-string-i-want-to-get
There's a function for that:
echo basename($url);
preg_split
Split string by a regular expression. Split the given string by a regular expression.
Your $url will be split by the dates. That's not the way you need to do:
<?php
$url = 'https://fakedomain.com/2017/07/01/the-string-i-want-to-get/';
$out = [];
preg_match('/\/\d{4}\/\d{2}\/\d{2}(.*)/', $url, $out);
// See here...
var_dump($out);
You will get an array of two elements:
array(2) {
[0]=>
string(37) "/2017/07/01/the-string-i-want-to-get/"
[1]=>
string(26) "/the-string-i-want-to-get/"
}
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.
Not working. I think I have looked at it too long and I'm blind to the problem:
<?php
$str = $_POST['data'];
$pattern = '#(www\.|https?:\/\/){?}[a-zA-Z0-9]{2,254}\.[a-zA-Z0-9]{2,4}(\S*)#i';
if (preg_match_all($pattern, $str, $matches, PREG_PATTERN_ORDER)) {
$uri = "($matches[1])";
}
$path = parse_url($uri, PHP_URL_PATH);
$output = substr(strrchr($path, '/'), 1);
?>
Sample embed code - I need the a695c468 part of the URL - it may change in length:
<iframe id="viddler-a695c468" src="//www.viddler.com/embed/a695c468/?f=1&offset=0&autoplay=0&secret=91361932&disablebranding=0&view_secret=91361932" width="545" height="349" frameborder="0" mozallowfullscreen="true" webkitallowfullscreen="true"></iframe>
Why parse it from URL? iframe id will be faster.
preg_match_all('/"viddler-(\w+)"/', $str, $matches, PREG_PATTERN_ORDER);
Updated:
Try this
preg_match_all('/www\.viddler\.com\/embed\/(\w+)\//', $str, $matches, PREG_PATTERN_ORDER);
So, I have this URL in a string:
http://www.domain.com/something/interesting_part/?somevars&othervars
in PHP, how I can get rid of all but interesting_part?
...
$url = 'http://www.domain.com/something/interesting_part/?somevars&othervars';
$parts = explode('/', $url);
echo $parts[4];
Output:
interesting_part
Try:
<?php
$url = 'http://www.domain.com/something/interesting_part/?somevars&othervars';
preg_match('`/([^/]+)/[^/]*$`', $url, $m);
echo $m[1];
You should use parse_url to do operations with URL. First parse it, then do changes you desire, using, for example, explode, then put it back together.
$uri = "http://www.domain.com/something/interesting_part/?somevars&othervars";
$uri_parts = parse_url( $uri );
/*
you should get:
array(4) {
["scheme"]=>
string(4) "http"
["host"]=>
string(14) "www.domain.com"
["path"]=>
string(28) "/something/interesting_part/"
["query"]=>
string(18) "somevars&othervars"
}
*/
...
// whatever regex or explode (regex seems to be a better idea now)
// used on $uri_parts[ "path" ]
...
$new_uri = $uri_parts[ "scheme" ] + $uri_parts[ "host" ] ... + $new_path ...
If the interesting part is always last part of path:
echo basename(parse_url($url, PHP_URL_PATH));
[+] please note that this will only work without index.php or any other file name before ?. This one will work for both cases:
$path = parse_url($url, PHP_URL_PATH);
echo ($path[strlen($path)-1] == '/') ? basename($path) : basename(dirname($path));
Here is example using parse_url() to override the specific part:
<?php
$arr = parse_url("http://www.domain.com/something/remove_me/?foo&bar");
$arr['path'] = "/something/";
printf("%s://%s%s?%s", $arr['scheme'], $arr['host'], $arr['path'], $arr['query']);