<?phpphp
header("Content-type:text/html;charset=utf-8"); html
?>數組
<form method="post" action="">ide
<input type="text" name="bl"/><input type="submit" value="計算"/>post
<p> 多個隨機比率英文,分割 例:50%,5%,0.5% </p>spa
</form>orm
<?phphtm
if(!empty($_POST)){排序
echo $_POST['bl'];//獲取值utf-8
echo "<hr/>";
$yarr = $arr = explode(',',$_POST['bl']);//字符串轉爲數組
print_r($arr);
echo "<hr/>";
rsort($arr);//數組按值重大到小排序
print_r($arr);
echo "<hr/>";
echo end($arr);//取最小值
echo "<hr/>";
$bl = end($arr);//取最小值賦值
echo trim($bl,'%');
echo "<hr/>";
echo strstr(trim($bl,'%'),'.');//去除%,並根據小數點截取
if(strstr(trim($bl,'%'),'.')){//去除%,並根據小數點截取,判斷是否截取到值
$cishu = strlen(strstr(trim($bl,'%'),'.'))-1;
$beishu = 10;
for($i=1;$i<$cishu;$i++){//小數點前移
$beishu = $beishu*10;
}
}else{
$beishu = 1;
}
echo $beishu;
echo "<hr/>";
$count = '';
$nums = '';
foreach($yarr as $val){
$count += $num[] = $nums = $val*$beishu+$nums;//取值範圍從新賦值
}
echo $count;
echo "<hr/>";
print_r($num);
echo "<hr/>";
$rand_num = rand(1,$beishu*100);//根據倍數換算,隨機取比率範圍內的值
$k = "";
foreach($num as $key=>$val){
if($key == 0){//判斷是不是第一次循環
if($rand_num >= 1 && $rand_num <= $val){//按範圍判斷是否出如今該中獎段
echo "恭喜你中獎了";
$k = $key;
break;
}
}else{
if($rand_num > $num[$key-1] && $rand_num <= $val){//取上一次的值和本次比較,判斷是否出如今該中獎段
echo "恭喜你中獎了";
$k = $key;
break;
}
}
}
echo $k;
}
?>