note 114307 added to function.parse-url

From: Date: Wed, 05 Feb 2014 16:56:13 +0000
Subject: note 114307 added to function.parse-url
Groups: php.notes 
Request: Send a blank email to php-notes+get-198414@lists.php.net to get a copy of this message
A simple function using "parse_url" to find the base URL of the given link. <?php function getPrimaryDomain($url) { $tld = parse_url($url,PHP_URL_HOST); $tldArray = explode(".",$tld); // COUNTS THE POSITION IN THE ARRAY TO IDENTIFY THE TOP LEVEL DOMAIN (TLD) $l1 = '0'; foreach($tldArray as $s) { // CHECKS THE POSITION IN THE ARRAY TO SEE IF IT MATCHES ANY OF THE KNOWN TOP LEVEL DOMAINS (YOU CAN ADD TO THIS LIST) if($s == 'com' || $s == 'net' || $s == 'info' || $s == 'biz' || $s == 'us' || $s == 'co' || $s == 'org' || $s == 'me') { // CALCULATES THE SECOND LEVEL DOMAIN POSITION IN THE ARRAY ONCE THE POSITION OF THE TOP LEVEL DOMAIN IS IDENTIFIED $l2 = $l1 - 1; } else { // INCREMENTS THE COUNTER FOR THE TOP LEVEL DOMAIN POSITION IF NO MATCH IS FOUND $l1++; } } // RETURN THE SECOND LEVEL DOMAIN AND THE TOP LEVEL DOMAIN IN THE FORMAT LIKE "SOMEDOMAIN.COM" echo $tldArray[$l2] . '.' . $tldArray[$l1]; } // CALL THE FUNCTION - THIS EXAMPLE RETURNS "BITLY.COM" getPrimaryDomain('http://www.enterprise.bitly.com/?utm_source=homepage); ?> ---- Server IP: 72.52.91.14 Probable Submitter: 72.52.91.14 ---- Manual Page -- http://php.net/manual/en/function.parse-url.php Edit -- https://master.php.net/note/edit/114307 Del: integrated -- https://master.php.net/note/delete/114307/integrated Del: useless -- https://master.php.net/note/delete/114307/useless Del: bad code -- https://master.php.net/note/delete/114307/bad+code Del: spam -- https://master.php.net/note/delete/114307/spam Del: non-english -- https://master.php.net/note/delete/114307/non-english Del: in docs -- https://master.php.net/note/delete/114307/in+docs Del: other reasons-- https://master.php.net/note/delete/114307 Reject -- https://master.php.net/note/reject/114307 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#198414) next »