How to obtain first subdomain from a domain name [duplicate] - php

This question already has answers here:
How to get the first subdomain with PHP?
(5 answers)
Closed 3 years ago.
I have the following code:
$url=$_SERVER['HTTP_HOST']; // www.abc.alpha.beta.xyz
$url=strtolower($url);
$rwww=str_replace("www.", "", $url);
But this results in abc.alpha.beta.xyz, the desired result is abc. How can I get just the first subdomain, ignoring www if present?

I think you can use the PHP strpos() function to check if the subdomain URL string contains the word alpha
// Search substring
$key = 'alpha';
$url = 'http://abc.alpha.beta.xyz';
if (strpos($url, $key) !== false) {
echo $key;
}
Not the best solution but might be helpful for you to get started.

There are a lot of ways to do that, a simple one being:
$host = $_SERVER['HTTP_HOST'] ?? 'www.abc.alpha.beta.xyz';
$parts = explode('.', $host);
$answer = null;
foreach ($parts as $subdomain) {
if ($subdomain === 'www') {
continue;
}
$answer = $subdomain;
break;
}
echo "The answer is '$answer'";
Which would output:
The answer is 'abc'
Be aware that this is a very naïve approach and will return example for the input www.example.com - which isn't a subdomain.

Related

Change URL Extension from given URL [duplicate]

This question already has answers here:
How to get host name from this kind of URL?
(2 answers)
Closed 8 years ago.
Is there any way to accept a URL and change it's domain to .com ?
For example if a user were to submit www.example.in, I want to check if the URL is valid, and change that to www.example.com. I have built a regex checker that can check if the URL is valid, but I'm not entirely sure how to check if the given extension is valid, and then to change it to .com
EDIT : To be clear I am not actually going to these URL's. I am getting them submitted as user input in a form, and am simply storing them. These are functions I want to do to the URL before storing, that is all.
Edit 2 : An example to make this clearer -
$url = 'www.example.co.uk'
$newurl = function($url);
echo $newurl
which would yield the output
www.example.com
Are you looking for something like this on the server side to replace a list of selected TLDs to be translated to .coms?
<?php
$url = "www.example.in";
$replacement_tld = "com";
# array of all TLDs you wish to support
$valid_tlds = array("in","co.uk");
# possible TLD source lists
# http://data.iana.org/TLD/tlds-alpha-by-domain.txt
# https://wiki.mozilla.org/TLD_List
# from http://stackoverflow.com/a/10473026/723139
function endsWith($haystack, $needle)
{
$haystack = strtolower($haystack);
$needle = strtolower($needle);
return $needle === "" || substr($haystack, -strlen($needle)) === $needle;
}
foreach($valid_tlds as $tld){
if(endsWith($url, $tld))
{
echo substr($url, 0, -strlen($tld)) . $replacement_tld . "\n";
break;
}
}
?>
Create an empty text file using a text editor such as notepad, and save it as htaccess.txt.
301 (Permanent) Redirect: Point an entire site to a different URL on a permanent basis. This is the most common type of redirect and is useful in most situations. In this example, we are redirecting to the "mt-example.com" domain:
# This allows you to redirect your entire website to any other domain
Redirect 301 / http://mt-example.com/
302 (Temporary) Redirect: Point an entire site to a different temporary URL. This is useful for SEO purposes when you have a temporary landing page and plan to switch back to your main landing page at a later date:
# This allows you to redirect your entire website to any other domain
Redirect 302 / http://mt-example.com/
For more details : http://kb.mediatemple.net/questions/242/How+do+I+redirect+my+site+using+a+.htaccess+file%3F
The question is not entirely clear, I'm assuming you wish to make this logic on PHP part.
Here's useful function to parse such strings:
function parseUrl ( $url )
{
$r = "^(?:(?P<scheme>\w+)://)?";
$r .= "(?:(?P<login>\w+):(?P<pass>\w+)#)?";
$r .= "(?P<host>(?:(?P<subdomain>[\w\.\-]+)\.)?" . "(?P<domain>\w+\.(?P<extension>\w+)))";
$r .= "(?::(?P<port>\d+))?";
$r .= "(?P<path>[\w/]*/(?P<file>\w+(?:\.\w+)?)?)?";
$r .= "(?:\?(?P<arg>[\w=&]+))?";
$r .= "(?:#(?P<anchor>\w+))?";
$r = "!$r!";
preg_match( $r, $url, $out );
return $out;
}
You can parse URL, validate it, and then recreate from resulting array replacing anything you want.
If you want to practice regexp and create own patterns - this site will be best place to do it.
If your goal to route users from one url to another or change URI style, then you need to use mod rewrite.
Actually in this case you will end up configuring your web server, probably virtual host, because it will route only listed domains (those being parked at the server).
To validate a URL in PHP You can use filter_var() .
filter_var($url, FILTER_VALIDATE_URL))
and then to get Top Level Domain (TLD) and replace the it with .com , you can use following function :
$url="http://www.dslreports.in";
$ext="com";
function change_url($url,$ext)
{
if(filter_var($url, FILTER_VALIDATE_URL)) {
$tld = '';
$url_parts = parse_url( (string) $url );
if( is_array( $url_parts ) && isset( $url_parts[ 'host' ] ) )
{
$host_parts = explode( '.', $url_parts[ 'host' ] );
if( is_array( $host_parts ) && count( $host_parts ) > 0 )
{
$tld = array_pop( $host_parts );
}
}
$new_url= str_replace($tld,$ext,$url);
return $new_url;
}else{
return "Not a valid URl";
}
}
echo change_url($url,$ext);
Hope this helps!

How to make sure sure that a string contains a valid/well formed url? [duplicate]

This question already has answers here:
What is the best regular expression to check if a string is a valid URL?
(62 answers)
Closed 9 years ago.
How to make sure that a string contains a valid/well formed url?
I need be sure the url in the string is well formed.
It must contain http:// or https://
and .com or .org or .net or any other valid extension
I tried some of the answers found here in SO but all are accepting "www.google.com" as valid.
In my case the valid url needs to be http://www.google.com or https://www.google.com.
The www. part is not an obligation, since some urls dont use it.
Take a look at the answer here:
PHP regex for url validation, filter_var is too permisive
filter_var() could be just fine for you, but if you need something more powerful, you'll have to use regex.
Additionally with the code from here, you can sub-in any regex that suits your needs:
<?php
$regex = "((https?|ftp)\:\/\/)?"; // SCHEME
$regex .= "([a-z0-9+!*(),;?&=\$_.-]+(\:[a-z0-9+!*(),;?&=\$_.-]+)?#)?"; // User and Pass
$regex .= "([a-z0-9-.]*)\.([a-z]{2,3})"; // Host or IP
$regex .= "(\:[0-9]{2,5})?"; // Port
$regex .= "(\/([a-z0-9+\$_-]\.?)+)*\/?"; // Path
$regex .= "(\?[a-z+&\$_.-][a-z0-9;:#&%=+\/\$_.-]*)?"; // GET Query
$regex .= "(#[a-z_.-][a-z0-9+\$_.-]*)?"; // Anchor
?>
Then, the correct way to check against the regex list as follows:
<?php
if(preg_match("/^$regex$/", $url))
{
return true;
}
?>
YOU can do this by using php filter_var function
$valid=filter_var($url, FILTER_VALIDATE_URL)
if($valid){
//your code
}
there's a curl solution:
function url_exists($url) {
if (!$fp = curl_init($url)) return false;
return true;
}
and there's a fopen solution (if you don't have
function url_exists($url) {
$fp = #fopen('http://example.com', 'r'); // #suppresses all error messages
if ($fp) {
// connection was made to server at domain example.com
fclose($fp);
return true;
}
return false;
}
filter_var($url, FILTER_VALIDATE_URL) can be first used to make sure you are dealing with a valid URL.
Then you have more conditions that can be tested by assuming the URL is indeed valid with parse_url:
$res = parse_url($url);
return ($res['scheme'] == 'http' || $ret['scheme'] == 'https') && $res['host'] != 'localhost');

How to retrieve complete URL from address bar using PHP? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to get full URL on the address bar using PHP
I use this function, but it does not work all the time. Can anyone give a hint?
function sofa_get_uri() {
$host = $_SERVER['SERVER_NAME'];
$self = $_SERVER["REQUEST_URI"];
$query = !empty($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : null;
$ref = !empty($query) ? "http://$host$self?$query" : "http://$host$self";
return $ref;
}
I want to retrieve the link in address bar (exactly) to use it to refer user back when he sign out. The urls are different:
http://domain.com/sample/address/?arg=bla
http://domain.com/?show=bla&act=bla&view=bla
http://domain.com/nice/permalinks/setup
But I can't get a function that works on all cases and give me the true referrer.
Hint please.
How about this?
function getAddress() {
$protocol = $_SERVER['HTTPS'] == 'on' ? 'https' : 'http';
return $protocol.'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
}
echo getAddress();
You could use functions above to retrieve URL till GET parameters.
So You have string like = 'localhost/site/tmp' (example).
After that you could just loop through GET parameters if can't get anything else to work.
Add '?' at the end of string manually.
$str = 'localhost/site/tmp/?'
foreach ($_GET as $key => $value) {
$str .= $key.'='.$value.'&';
}
substr_replace($str, "", -1);
echo $str;
At the end You are deleting last symbol which is '&' and is not needed.

Check whether url contains http:// or https:// [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Check if the url is contains the http or https
What is the code to figure out whether the given URL contains http:// or https:// at the beginning using PHP and regular expressions?
you can use parse_url
<?php
$url = parse_url('https://example.org');
if ($url['scheme'] == 'https') {
// is https;
}
?>
if (substr($string, 0, 7) == "http://") {
$res = "http";
}
if (substr($string, 0, 8) == "https://") {
$res = "https";
}
Maybe this could help
$_SERVER['SERVER_PROTOCOL'];

PHP function to get the subdomain of a URL

Is there a function in PHP to get the name of the subdomain?
In the following example I would like to get the "en" part of the URL:
en.example.com
Here's a one line solution:
array_shift((explode('.', $_SERVER['HTTP_HOST'])));
Or using your example:
array_shift((explode('.', 'en.example.com')));
EDIT: Fixed "only variables should be passed by reference" by adding double parenthesis.
EDIT 2: Starting from PHP 5.4 you can simply do:
explode('.', 'en.example.com')[0];
Uses the parse_url function.
$url = 'http://en.example.com';
$parsedUrl = parse_url($url);
$host = explode('.', $parsedUrl['host']);
$subdomain = $host[0];
echo $subdomain;
For multiple subdomains
$url = 'http://usa.en.example.com';
$parsedUrl = parse_url($url);
$host = explode('.', $parsedUrl['host']);
$subdomains = array_slice($host, 0, count($host) - 2 );
print_r($subdomains);
You can do this by first getting the domain name (e.g. sub.example.com => example.co.uk) and then use strstr to get the subdomains.
$testArray = array(
'sub1.sub2.example.co.uk',
'sub1.example.com',
'example.com',
'sub1.sub2.sub3.example.co.uk',
'sub1.sub2.sub3.example.com',
'sub1.sub2.example.com'
);
foreach($testArray as $k => $v)
{
echo $k." => ".extract_subdomains($v)."\n";
}
function extract_domain($domain)
{
if(preg_match("/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i", $domain, $matches))
{
return $matches['domain'];
} else {
return $domain;
}
}
function extract_subdomains($domain)
{
$subdomains = $domain;
$domain = extract_domain($subdomains);
$subdomains = rtrim(strstr($subdomains, $domain, true), '.');
return $subdomains;
}
Outputs:
0 => sub1.sub2
1 => sub1
2 =>
3 => sub1.sub2.sub3
4 => sub1.sub2.sub3
5 => sub1.sub2
http://php.net/parse_url
<?php
$url = 'http://user:password#sub.hostname.tld/path?argument=value#anchor';
$array=parse_url($url);
$array['host']=explode('.', $array['host']);
echo $array['host'][0]; // returns 'sub'
?>
As the only reliable source for domain suffixes are the domain registrars, you can't find the subdomain without their knowledge.
There is a list with all domain suffixes at https://publicsuffix.org. This site also links to a PHP library: https://github.com/jeremykendall/php-domain-parser.
Please find an example below. I also added the sample for en.test.co.uk which is a domain with a multi suffix (co.uk).
<?php
require_once 'vendor/autoload.php';
$pslManager = new Pdp\PublicSuffixListManager();
$parser = new Pdp\Parser($pslManager->getList());
$host = 'http://en.example.com';
$url = $parser->parseUrl($host);
echo $url->host->subdomain;
$host = 'http://en.test.co.uk';
$url = $parser->parseUrl($host);
echo $url->host->subdomain;
PHP 7.0: Use the explode function and create a list of all the results.
list($subdomain,$host) = explode('.', $_SERVER["SERVER_NAME"]);
Example: sub.domain.com
echo $subdomain;
Result: sub
echo $host;
Result: domain
Simply...
preg_match('/(?:http[s]*\:\/\/)*(.*?)\.(?=[^\/]*\..{2,5})/i', $url, $match);
Just read $match[1]
Working example
It works perfectly with this list of urls
$url = array(
'http://www.domain.com', // www
'http://domain.com', // --nothing--
'https://domain.com', // --nothing--
'www.domain.com', // www
'domain.com', // --nothing--
'www.domain.com/some/path', // www
'http://sub.domain.com/domain.com', // sub
'опубликованному.значения.ua', // опубликованному ;)
'значения.ua', // --nothing--
'http://sub-domain.domain.net/domain.net', // sub-domain
'sub-domain.third-Level_DomaIN.domain.uk.co/domain.net' // sub-domain
);
foreach ($url as $u) {
preg_match('/(?:http[s]*\:\/\/)*(.*?)\.(?=[^\/]*\..{2,5})/i', $u, $match);
var_dump($match);
}
Simplest and fastest solution.
$sSubDomain = str_replace('.example.com','',$_SERVER['HTTP_HOST']);
$REFERRER = $_SERVER['HTTP_REFERER']; // Or other method to get a URL for decomposition
$domain = substr($REFERRER, strpos($REFERRER, '://')+3);
$domain = substr($domain, 0, strpos($domain, '/'));
// This line will return 'en' of 'en.example.com'
$subdomain = substr($domain, 0, strpos($domain, '.'));
Using regex, string functions, parse_url() or their combinations it's not real solution. Just test any of proposed solutions with domain test.en.example.co.uk, there will no any correct result.
Correct solution is use package that parses domain with Public Suffix List. I recomend TLDExtract, here is sample code:
$extract = new LayerShifter\TLDExtract\Extract();
$result = $extract->parse('test.en.example.co.uk');
$result->getSubdomain(); // will return (string) 'test.en'
$result->getSubdomains(); // will return (array) ['test', 'en']
$result->getHostname(); // will return (string) 'example'
$result->getSuffix(); // will return (string) 'co.uk'
What I found the best and short solution is
array_shift(explode(".",$_SERVER['HTTP_HOST']));
For those who get 'Error: Strict Standards: Only variables should be passed by reference.'
Use like this:
$env = (explode(".",$_SERVER['HTTP_HOST']));
$env = array_shift($env);
$domain = 'sub.dev.example.com';
$tmp = explode('.', $domain); // split into parts
$subdomain = current($tmp);
print($subdomain); // prints "sub"
As seen in a previous question:
How to get the first subdomain with PHP?
There isn't really a 100% dynamic solution - I've just been trying to figure it out as well and due to different domain extensions (DTL) this task would be really difficult without actually parsing all these extensions and checking them each time:
.com vs .co.uk vs org.uk
The most reliable option is to define a constant (or database entry etc.) that stores the actual domain name and remove it from the $_SERVER['SERVER_NAME'] using substr()
defined("DOMAIN")
|| define("DOMAIN", 'mymaindomain.co.uk');
function getSubDomain() {
if (empty($_SERVER['SERVER_NAME'])) {
return null;
}
$subDomain = substr($_SERVER['SERVER_NAME'], 0, -(strlen(DOMAIN)));
if (empty($subDomain)) {
return null;
}
return rtrim($subDomain, '.');
}
Now if you're using this function under http://test.mymaindomain.co.uk it will give you test or if you have multiple sub-domain levels http://another.test.mymaindomain.co.uk you'll get another.test - unless of course you update the DOMAIN.
I hope this helps.
Simply
reset(explode(".", $_SERVER['HTTP_HOST']))
I'm doing something like this
$url = https://en.example.com
$splitedBySlash = explode('/', $url);
$splitedByDot = explode('.', $splitedBySlash[2]);
$subdomain = $splitedByDot[0];
Suppose current url = sub.example.com
$host = array_reverse(explode('.', $_SERVER['SERVER_NAME']));
if (count($host) >= 3){
echo "Main domain is = ".$host[1].".".$host[0]." & subdomain is = ".$host[2];
// Main domain is = example.com & subdomain is = sub
} else {
echo "Main domain is = ".$host[1].".".$host[0]." & subdomain not found";
// "Main domain is = example.com & subdomain not found";
}
this is my solution, it works with the most common domains, you can fit the array of extensions as you need:
$SubDomain = explode('.', explode('|ext|', str_replace(array('.com', '.net', '.org'), '|ext|',$_SERVER['HTTP_HOST']))[0]);
// For www.abc.en.example.com
$host_Array = explode(".",$_SERVER['HTTP_HOST']); // Get HOST as array www, abc, en, example, com
array_pop($host_Array); array_pop($host_Array); // Remove com and exmaple
array_shift($host_Array); // Remove www (Optional)
echo implode($host_Array, "."); // Combine array abc.en
I know I'm really late to the game, but here goes.
What I did was take the HTTP_HOST server variable ($_SERVER['HTTP_HOST']) and the number of letters in the domain (so for example.com it would be 11).
Then I used the substr function to get the subdomain. I did
$numberOfLettersInSubdomain = strlen($_SERVER['HTTP_HOST'])-12
$subdomain = substr($_SERVER['HTTP_HOST'], $numberOfLettersInSubdomain);
I cut the substring off at 12 instead of 11 because substrings start on 1 for the second parameter. So now if you entered test.example.com, the value of $subdomain would be test.
This is better than using explode because if the subdomain has a . in it, this will not cut it off.
if you are using drupal 7
this will help you:
global $base_path;
global $base_root;
$fulldomain = parse_url($base_root);
$splitdomain = explode(".", $fulldomain['host']);
$subdomain = $splitdomain[0];
$host = $_SERVER['HTTP_HOST'];
preg_match("/[^\.\/]+\.[^\.\/]+$/", $host, $matches);
$domain = $matches[0];
$url = explode($domain, $host);
$subdomain = str_replace('.', '', $url[0]);
echo 'subdomain: '.$subdomain.'<br />';
echo 'domain: '.$domain.'<br />';
From PHP 5.3 you can use strstr() with true parameter
echo strstr($_SERVER["HTTP_HOST"], '.', true); //prints en
Try this...
$domain = 'en.example.com';
$tmp = explode('.', $domain);
$subdomain = current($tmp);
echo($subdomain); // echo "en"
function get_subdomain($url=""){
if($url==""){
$url = $_SERVER['HTTP_HOST'];
}
$parsedUrl = parse_url($url);
$host = explode('.', $parsedUrl['path']);
$subdomains = array_slice($host, 0, count($host) - 2 );
return implode(".", $subdomains);
}
you can use this too
echo substr($_SERVER['HTTP_HOST'], 0, strrpos($_SERVER['HTTP_HOST'], '.', -5));
Maybe I'm late, but even though the post is old, just as I get to it, many others do.
Today, the wheel is already invented, with a library called php-domain-parser that is active, and in which two mechanisms can be used.
One based on the Public Suffix List and one based on the IANA list.
Simple and effective, it allows us to create simple helpers that help us in our project, with the ability to know that the data is maintained, in a world in which the extensions and their variants are very changeable.
Many of the answers given in this post do not pass a battery of unit tests, in which certain current extensions and their variants with multiple levels are checked, and neither with the casuistry of domains with extended characters.
Maybe it serves you, as it served me.
<?php
// Your code here!
function get_domain($host) {
$parts = explode('.',$host);
$extension = $parts[count($parts)-1];
$name = $parts[count($parts)-2];
return $name.'.'.$extension;
}
echo get_domain("https://api.neoistone.com");
?>
If you only want what comes before the first period:
list($sub) = explode('.', 'en.example.com', 2);

Categories