易于理解的我的功能有逻辑缺陷


Simple? Logic flaw with my function

我有一个问题,当你阅读我的函数时,它可能会变得很明显,但我不知道该怎么办。

问题是,我需要使用"do,while",因为我需要"do"的结果,以在"while"中进行测试。问题是,当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;
  }
}

谢谢你提供的任何帮助。

用一种可以区分这些字符的字体来表示代码不是更容易吗?也就是说,只需使用正则表达式来"简化"多个字符串匹配:

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

从使用中删除这些字符只会使你更有可能得到一张重复的代金券。