很多时候需要在网站显示Alexa排名,但讨厌了采用工具栏的方式和浏览器扩展的方式,于是就有自己去看看获取Alexa排名的想法。参考一些资料后,算是了解了Alexa排名的获取了,于是将该算法分享出来。
获取Alexa排名程序如下:
<?php
/**
* 获取Alexa排名
* 使用方式一:
* URL: http://www.phpcoding.cn/lib/alexa.php?url=http://www.phpcoding.cn
* 说明:将URL中的url=后面的网站地址换成相应的网站地址即可,可直接显示为: Alexa: xxxxx。
* 使用方式二:
* URL:http://www.phpcoding.cn/lib/alexa.php?pic=pic&url=http://www.phpcoding.cn
* 说明:与方式一一样,将最后的网站地址换成目标网站地址即可。但此时应采用如下方式进行使用:
* <script language="javascript" src="http://www.phpcoding.cn/lib/alexa.php?pic=pic&url=http://www.phpcoding.cn">
* 这样才能够在页面上显示为图形方式
* @author achao.org
* @see http://www.phpcoding.cn
*/
if (isset($_GET['url'])) {
$url = $_GET['url'];
}else {
$url = "http://www.phpcoding.cn";
}
$rank = trim(getAlexaRank ( $url ));
if (!isset($_GET['pic'])) {
echo "Alexa: " . $rank;
}else {
echo "document.write('<table width="60" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC" style="font-size:11px;" title="Alexa:{$rank}"><tr bgcolor="#FFFFFF">";
echo "<td style="border:1px solid #999999;height:8px;width:80px;">{$rank}</td></tr></table>');";
}
function getAlexaRank($weburl) {
$weburl = strtolower ( $weburl );
$tempurl = getDomainUrl ( $weburl );
$strAlexaCss = file_get_contents ( 'http://client.alexa.com/common/css/scramble.css' ); //
$alexaRankQueryUrl = 'http://www.alexa.com/data/details/traffic_details/' . $tempurl;
$strAlexaContent = file_get_contents ( $alexaRankQueryUrl );
$rankContent = getBody ( $strAlexaContent, 'Information Service.-->', '<!-- google_ad_section_end(name=default) -->' );
$arrSpanClass = getArray ( $rankContent, '<span class="', '">' );
foreach ( $arrSpanClass as $css ) {
if (strpos ( $strAlexaCss, '.' . $css ) > 0)
$rankContent = ScriptHtml ( $rankContent, "span", 2, $css );
else
$rankContent = ScriptHtml ( $rankContent, "span", 1, $css );
}
$rankContent = str_replace ( '</span>', '', $rankContent );
$rankContent = str_replace ( ',', '', $rankContent );
return $rankContent;
}
function getBody($ContentStr, $StartStr, $EndStr) {
$ContentStr = strtolower ( $ContentStr );
$StartStr = strtolower ( $StartStr );
$EndStr = strtolower ( $EndStr );
$StartPos = strpos ( $ContentStr, $StartStr );
$EndPos = strpos ( $ContentStr, $EndStr );
return substr ( $ContentStr, $StartPos + strlen ( $StartStr ), $EndPos - $StartPos - strlen ( $StartStr ) );
}
function getArray($ContentStr, $StartStr, $EndStr) {
$reg = '/' . $StartStr . '.+?' . $EndStr . '/';
preg_match_all ( $reg, $ContentStr, $arr, PREG_PATTERN_ORDER );
for($i = 0; $i < count ( $arr [0] ); $i ++) {
$arr [0] [$i] = str_replace ( $EndStr, '', str_replace ( $StartStr, '', $arr [0] [$i] ) );
}
return $arr [0];
}
function getDomainUrl($url) {
$tempUrl = str_replace ( 'http://', '', $url );
$tempUrl = str_replace ( '/', '', $tempUrl );
return $tempUrl;
}
function ScriptHtml($ContentStr, $TagName, $FType, $includestr) {
switch ($FType) {
case 1 :
$Pattern = '/<' . $TagName . '([^>])*(' . $includestr . '){1,}([^>])*>/i';
$ContentStr = preg_replace ( $Pattern, '', $ContentStr );
break;
case 2 :
$Pattern = '/< ' . $TagName . '([^>])*(' . $includestr . '){1,}([^>])*>.*?< /' . $TagName . '([^>])*>/i';
$ContentStr = preg_replace ( $Pattern, '', $ContentStr );
break;
case 3 :
$Pattern = '/< ' . $TagName . '([^>])*(' . $includestr . '){1,}([^>])*>/i';
$ContentStr = preg_replace ( $Pattern, '', $ContentStr );
$Pattern = '/< /' . $TagName . '([^>])*>/i';
$ContentStr = preg_replace ( $Pattern, '', $ContentStr );
break;
default :
echo 'error';
}
return $ContentStr;
}
?>
其使用方法见里面说明。
PHP教程
alexa, alexa算法, 获取Alexa排名
最近评论