Fix getDomainFromURL
This commit is contained in:
parent
cd32734c58
commit
454316a5fa
@ -172,14 +172,18 @@ function getURLContent($url, $postorget = 'GET', $param = '', $followlocation =
|
||||
* For example: https://www.abc.mydomain.com/dir/page.html return 'mydomain'
|
||||
*
|
||||
* @param string $url Full URL.
|
||||
* @param string $mode 0=return 'mydomain', 1=return 'mydomain.com'
|
||||
* @return string Returns domaine name
|
||||
*/
|
||||
function getDomainFromURL($url)
|
||||
function getDomainFromURL($url, $mode=0)
|
||||
{
|
||||
$tmpdomain = preg_replace('/^https?:\/\//i', '', $url); // Remove http(s)://
|
||||
$tmpdomain = preg_replace('/\/.*$/i', '', $tmpdomain); // Remove part after domain
|
||||
$tmpdomain = preg_replace('/\.[^\.]+$/', '', $tmpdomain); // Remove first level domain (.com, .net, ...)
|
||||
$tmpdomain = preg_replace('/^[^\.]+\./', '', $tmpdomain); // Remove part www. before domain name
|
||||
$tmpdomain = preg_replace('/^.*\.([^\.]+)\.([^\.]+)$/', '\1.\2', $tmpdomain); // Remove part www.abc before domain name
|
||||
if (empty($mode))
|
||||
{
|
||||
$tmpdomain = preg_replace('/\.[^\.]+$/', '', $tmpdomain); // Remove first level domain (.com, .net, ...)
|
||||
}
|
||||
|
||||
return $tmpdomain;
|
||||
}
|
||||
|
||||
@ -46,7 +46,7 @@ $conf->global->MAIN_DISABLE_ALL_MAILS=1;
|
||||
* @backupStaticAttributes enabled
|
||||
* @remarks backupGlobals must be disabled to have db,conf,user and lang not erased.
|
||||
*/
|
||||
class GetUrlLibTest extends PHPUnit_Framework_TestCase
|
||||
class GetUrlLibTest extends PHPUnit\Framework\TestCase
|
||||
{
|
||||
protected $savconf;
|
||||
protected $savuser;
|
||||
@ -167,6 +167,45 @@ class GetUrlLibTest extends PHPUnit_Framework_TestCase
|
||||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* testGetDomainFromURL
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function testGetDomainFromURL()
|
||||
{
|
||||
global $conf,$user,$langs,$db;
|
||||
$conf=$this->savconf;
|
||||
$user=$this->savuser;
|
||||
$langs=$this->savlangs;
|
||||
$db=$this->savdb;
|
||||
|
||||
$result=getDomainFromURL('https://dolimed.com');
|
||||
print __METHOD__." result=".$result."\n";
|
||||
$this->assertEquals('dolimed', $result, 'Test 1');
|
||||
|
||||
$result=getDomainFromURL('http://www.dolimed.com/screenshots/afile');
|
||||
print __METHOD__." result=".$result."\n";
|
||||
$this->assertEquals('dolimed', $result, 'Test 2');
|
||||
|
||||
$result=getDomainFromURL('http://www.with.dolimed.com/screenshots/afile');
|
||||
print __METHOD__." result=".$result."\n";
|
||||
$this->assertEquals('dolimed', $result, 'Test 3');
|
||||
|
||||
$result=getDomainFromURL('https://dolimed.com', 1);
|
||||
print __METHOD__." result=".$result."\n";
|
||||
$this->assertEquals('dolimed.com', $result, 'Test 4');
|
||||
|
||||
$result=getDomainFromURL('http://www.dolimed.com/screenshots/afile', 1);
|
||||
print __METHOD__." result=".$result."\n";
|
||||
$this->assertEquals('dolimed.com', $result, 'Test 5');
|
||||
|
||||
$result=getDomainFromURL('http://www.with.dolimed.com/screenshots/afile', 1);
|
||||
print __METHOD__." result=".$result."\n";
|
||||
$this->assertEquals('dolimed.com', $result, 'Test 6');
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* testRemoveHtmlComment
|
||||
|
||||
Loading…
Reference in New Issue
Block a user