学PHP没多久,突然想到怎样来实现这个,只当是锻炼锻炼,假设等级图片地址如下(晕,貌似只有登录QQ网站后才能显示这几个图片)
http://www.myexception.cn/img/2013/01/16/1107071118.png 皇冠
http://www.myexception.cn/img/2013/01/16/1107071119.png 太阳
http://www.myexception.cn/img/2013/01/16/1107071120.png 月亮
http://www.myexception.cn/img/2013/01/16/1107071121.png 星星
我已经写好了一个
$a = base_convert(85,10,4); //85为数字等级
$b = array(1=>'star',2=>'moon',3=>'sun',4=>'crown');
$e = strlen($a);
for($i=0;$i<$e;$i++){
$c = (int)substr($a,$i,1);
if($c>0) for($d=0;$d<$c;$d++) echo '<img src="http://id.qq.com/level/img/'.$b[$e-$i].'.png" />';
}
主要是利用十进制转四进制来实现,不知道哪位还有更好的办法来实现呢?
------解决方案--------------------
if(isset($_GET['img'])) {
die(file_get_contents("http://id.qq.com/level/img/$_GET[img].png"));
}
$a = base_convert(85,10,4); //85为数字等级
$b = array(1=>'star',2=>'moon',3=>'sun',4=>'crown');
$e = strlen($a);
for($i=0;$i<$e;$i++){
$c = (int)substr($a,$i,1);
if($c>0) for($d=0;$d<$c;$d++) echo '<img src="?img='.$b[$e-$i].'" />';
}
------解决方案--------------------
<?php
function qqdj($nowlevel)
{
/*星星1 月亮4 太阳16 皇冠64 */
$a = $b = $c = $d = 0;
$d = floor($nowlevel/64);
$dj= $nowlevel%64;
$c = floor($dj/16);
$dj = $dj%16;
$b = floor($dj/4);
$a = $dj%4;
return array($a,$b,$c,$d);
}
function qqdjImg($nowlevel)
{
/*星星1 月亮4 太阳16 皇冠64 */
$arrDJ = array();
$arrDJ[] = floor($nowlevel/64);
$dj= $nowlevel%64;
$arrDJ[] = floor($dj/16);
$dj = $dj%16;
$arrDJ[] = floor($dj/4);
$arrDJ[] = $dj%4;
$arrImg = array(
'<img src="http://id.qq.com/level/img/crown.png" />',
'<img src="http://id.qq.com/level/img/sun.png" />',
'<img src="http://id.qq.com/level/img/moon.png" />',
'<img src="http://id.qq.com/level/img/star.png" />',
);
$html = '';
foreach ($arrDJ as $k => $v)
{
$html .= str_repeat($arrImg[$k],$v);
}
print_r($arrDJ);
return $html;
}
$dj = 37;
$dj = 71;
echo qqdjImg($dj);
/*
X-Powered-By: PHP/5.2.0
Content-type: text/html
Array
(
[0] => 1
[1] => 0
[2] => 1
[3] => 3
)
<img src="http://id.qq.com/level/img/crown.png" /><img src="http://id.qq.com/level/img/moon.png" /><img src="http://id.qq.com/level/img/star.png" /><img src="http://id.qq.com/level/img/star.png" /><img src="http://id.qq.com/level/img/star.png" />