PHP随机混合大小写


php random mixed alpha cases

我想看到另一种方式来转换字符串中的一些字符混合的情况下,我认为我的方式不是最佳的方式…

$arr_str = str_split("w2abcd");
$atCase = "";
foreach ($arr_str as $cha) {
    $toup =  rand(0, 1);
    if($toup == 1){ $atCase .= ucfirst($cha); } else { $atCase .=  $cha;}
}
    $rtnstr = $atCase;

看起来不错。优化可能是这样的:

$str = "w2abcd";
for ($i=0,$c=strlen($str);$i<$c;$i++)
  $str[$i] = rand(0, 100) > 50?$strtoupper($str[$i]):$str[$i];
return $str;

好吧,这只是我的变体:

<?php
$str = str_split(strtolower('some text'));
foreach ($str as &$char)
{
    if (rand(0, 1)) $char = strtoupper($char);
}
print implode('', $str);