php 自訂驗證碼函數的有關問題

來源:互聯網
上載者:User
php 自訂驗證碼函數的問題
請教各位一個問題:
php 自訂驗證碼函數
function random_text($count, $rm_similar = false)
{
$chars = array_flip(array_merge(range(0, 9), range('A', 'Z')));
if ($rm_similar)
{
unset($chars[0], $chars[1], $chars[2], $chars[5], $chars[8],
$chars['B'], $chars['I'], $chars['O'], $chars['Q'],
$chars['S'], $chars['U'], $chars['V'], $chars['Z']);
}

for ($i = 0, $text = ''; $i < $count; $i++)
{
$text .= array_rand($chars);
}
return $text;
}
?>
這段代碼是什麼作用了,$chars[5]這些是啥意思:unset($chars[0], $chars[1], $chars[2], $chars[5], $chars[8],
$chars['B'], $chars['I'], $chars['O'], $chars['Q'],
$chars['S'], $chars['U'], $chars['V'], $chars['Z']);
這個函數調用的話,怎麼調用了,給個例子。 php

分享到:


------解決方案--------------------
function random_text($count, $rm_similar = false)
{
$chars = array_flip(array_merge(range(0, 9), range('A', 'Z')));//產生一個二維數組[0-9][A-Z]數組的值依次遞增 比如['A']=10,['B']=11 ....
if ($rm_similar)
{
unset($chars[0], $chars[1], $chars[2], $chars[5], $chars[8],
$chars['B'], $chars['I'], $chars['O'], $chars['Q'],
$chars['S'], $chars['U'], $chars['V'], $chars['Z']);
//根據參數$rm_similar(預設為false)來判斷是否釋放上面這些數組的值
}

for ($i = 0, $text = ''; $i < $count; $i++)
{
$text .= array_rand($chars);//根據$count來隨機取的數組中的值
}
return $text;
}
?>


樣本:

//1.取7位文本代碼,並去除unset裡的值
random_text(7,true);

//2.取5位文本代碼,不去除unset裡的值,這裡第二個參數按函數預設的來即為false
random_text(5);

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.