How extract part of an URL in PHP to remove specific part? - php

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']);

Related

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 regex doesn't seem to work as expected

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/"
}

get only "google" from a full google url with subdomains in php

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.

Remove last child page from URI

How can one dynamically find and remove the last child of a website path URI?
Code: $uri = $_SERVER["REQUEST_URI"];
Result: http://192.168.0.16/wordpress/blog/page-2/
Desired result: http://192.168.0.16/wordpress/blog/
Many thanks in advance!
you can use this and you can get your required output:
// implode string into array
$url = "http://192.168.0.16/wordpress/blog/page-2/";
//then remove character from right
$url = rtrim($url, '/');
// then explode
$url = explode('/', $url);
// remove the last element and return an array
json_encode(array_pop($url));
// implode again into string
echo implode('/', $url);
another approach is:
// implode string into array
$url = explode('/', 'http://192.168.0.16/wordpress/blog/page-2/');
//The array_filter() function filters the values of an array using a callback function.
$url = array_filter($url);
// remove the last element and return an array
array_pop($url);
// implode again into string
echo implode('/', $url);
$url = 'http://192.168.0.16/wordpress/blog/page-2/';
// trim any slashes at the end
$trim_url = rtrim($url,'/');
// explode with slash
$url_array = explode('/', $trim_url);
// remove last element
array_pop($url_array);
// implade with slash
echo $new_url = implode('/', $url_array);
Output:
http://192.168.0.16/wordpress/blog
The correct way would be to use parse_url() and dirname(), which will also support query params. You could explode $uri['path'] but its unnecessary in this case.
<?php
// explode the uri in its proper parts
$uri = parse_url('/wordpress/blog/page-2/?id=bla');
// remove last element
$path = dirname($uri['path']);
// incase you got query params, append them
if (!empty($uri['query'])) {
$path .= '?'.$uri['query'];
}
// string(22) "/wordpress/blog?id=bla"
var_dump($path);
See it working: https://3v4l.org/joJrF

Get Root Domain Name and subdomain Of Website using PHP

I am using this function code
public function getRootDomain($domain)
{
$domain = explode('.', $domain);
$tld = array_pop($domain);
$name = array_pop($domain);
$domain = "$name.$tld";
return $domain;
}
And the output i get is something like example.com
but i want to show m.example.com or www.example.com
Help related this ..thankx
Use parse_url(). You would want the host:
<?php
$url = '//www.example.com/path?googleguy=googley';
// Prior to 5.4.7 this would show the path as "//www.example.com/path"
var_dump(parse_url($url));
?>
The above example will output:
array(3) {
["host"]=>
string(15) "www.example.com"
["path"]=>
string(5) "/path"
["query"]=>
string(17) "googleguy=googley"
}
You would use it like so:
public function getRootDomain($domain)
{
$parts = parse_url($domain);
return $parts['host'];
}
If you're using PHP 5.4+:
public function getRootDomain($domain)
{
return parse_url($domain)['host'];
}

Categories