[PHP] GetAlexaTrafficRank

Status
Not open for further replies.

Vick

Banned
Banned
297
2011
72
0
Apparently I don't contribute to WJ. This source has been removed because personal reasons of me not wanting to share.

Good day.
 
Last edited:
6 comments
PHP:
<?php

/**
 * This is simple description.
 * 
 * @author Vick Kumar <vickkumar2011@gmail.com>
 * @copyright Seecure.me, 2012
 * @version 1.0
 * @license http://creativecommons.org/licenses/by/3.0/legalcode 
 */
$domain = 'wjunction.com';

function GetAlexaTrafficRank($domain)
{
        $source = file_get_contents("http://www.alexa.com/siteinfo/$domain#");
        $regex = '/is ranked (.*) in/';
        preg_match($regex, $source, $match);
        $newresult = str_replace("number", "", $match[1]);
        return $newresult;
}
echo $domain." 's Alexa Traffic Rank is: ".GetAlexaTrafficRank($domain).".";
?>
Fixed :)

Easier for them noobs.
 
Last edited:
PHP:
function GetAlexaTrafficRank($domain) {
   $domain = preg_replace('#(https?://)?(www\.)?#i','',$domain);
   ...
This should strip "http://", "https://", "www.", so you can pass almost any domain to the function.
 
The Regex failed :P

PHP:
<?php

/**
 * This is simple description.
 * 
 * @author Vick Kumar <vickkumar2011@gmail.com>
 * @copyright Seecure.me, 2012
 * @version 1.0
 * @license http://creativecommons.org/licenses/by/3.0/legalcode 
 */

function GetAlexaTrafficRank($domain)
{
        $source = file_get_contents("http://www.alexa.com/siteinfo/$domain#");
        $regex = '/is ranked (.*) in/';
        preg_match($regex, $source, $match);
        $newresult = str_replace("number", "", $match[1]);
        return $newresult;
}
echo "Wjunction's Alexa Traffic Rank is: ".GetAlexaTrafficRank("wjunction.com").".";
?>

Try it yourself and heres the result:
SgrHg.png


Fix the regex
 
http://www.alexa.com/siteinfo/wjunction.com
The rank is calculated using a combination of average daily visitors to
wjunction.com and pageviews on wjunction.com over the past 3 months. The site with the highest combination of visitors and pageviews is ranked #1.
That's why the regex fails.
Here's a fix:
PHP:
function GetAlexaTrafficRank($domain)
{
        $source = file_get_contents("http://www.alexa.com/siteinfo/$domain#");
        $regex = '#([0-9,]+)\s*</div>\s*<div class="label">Global Rank#';
        preg_match($regex, $source, $match);
        $newresult = str_replace("number", "", $match[1]);
        return $newresult;
}
echo "Wjunction's Alexa Traffic Rank is: ".GetAlexaTrafficRank("wjunction.com").".";
 
Status
Not open for further replies.
Back
Top