2012-03-13 58 views
0

我有一個問題,它可能會變得明顯,當你閱讀我的功能,但我不知道該怎麼做。簡單嗎?邏輯瑕疵與我的功能

問題是,我需要使用「do,while」,因爲我需要「do」的結果來測試「while」。問題是,當所有4個條件都返回false時,它退出,但是這留下了一個錯誤的代碼。

我需要重新生成一個代碼,直到它包含沒有不可區分的字符。

function make_code(){ 
    do{ 
     $prefix   = mt_rand(0, mt_getrandmax()); 
     $code   = uniqid($prefix);//good to make sure we always have a unique string to work with, even with no seed supplied. 
     $sha1   = sha1($code); 
     $base_convert = base_convert($sha1, 16, 36);//expand hex with the rest of the alphabet. 
     $substr   = substr($base_convert, 0, 12);//we only want the first 12 characters. 
     $strtoupper  = strtoupper($substr);//for aesthetics. 
     $str_split  = str_split($strtoupper, 4);//seperate into chunks. 
     $voucher_code = $str_split[0] . self::CS . $str_split[1] . self::CS . $str_split[2];//build 
    } 
    while(
      (stristr($voucher_code, "o") === false) 
     && (stristr($voucher_code, "0") === false) 
     && (stristr($voucher_code, "1") === false) 
     && (stristr($voucher_code, "i") === false)); 


    return $voucher_code; 
    } 
} 

感謝您提供任何幫助。

回答

2

將這些代碼呈現在能夠使這些字符可區分的字體中是不是更容易?話雖這麼說,簡單地使用正則表達式來「簡化」的多字符串匹配:

do { 
    ... 
while (preg_match('/[01lo]/i', $voucher_code)); 

消除使用這些字符只是使它成爲更可能你會重複憑證結束。

+0

嗨,馬克。這些代碼將打印在帶有刮板的塑料卡上,並在超市銷售。字體是不是很定製,這不是我的舞臺anyhoo ... – 2012-03-13 15:06:07

+0

你preg_match確實解決了邏輯問題,它的工作,但它顯示大寫字符,即使它應該不區分大小寫。 :S – 2012-03-13 15:07:58

+0

這就是'/ i'的結尾 - 使匹配不區分大小寫。 – 2012-03-13 15:09:54