开发者

php random mixed alpha cases

开发者 https://www.devze.com 2023-04-04 05:47 出处:网络
I would to see another way to convert some of the character in the string to mixed case i think my way is not the optimal way..

I would to see another way to convert some of the character in the string to mixed case i think my way is not the optimal way..

$arr_str = str_split("w2abcd");

$atCase = "";
foreach ($arr_str as $cha) {
    $toup =  rand(0, 1);

    if($toup == 1开发者_JAVA百科){ $atCase .= ucfirst($cha); } else { $atCase .=  $cha;}
}
    $rtnstr = $atCase;


looks pretty good. the optimization may be like this:

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


Well, just my variant:

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

精彩评论

暂无评论...
验证码 换一张
取 消