0

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){ $atCase .= ucfirst($cha); } else { $atCase .=  $cha;}
}
    $rtnstr = $atCase;
david
  • 4,218
  • 3
  • 23
  • 25

2 Answers2

3

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;
heximal
  • 10,327
  • 5
  • 46
  • 69
  • i have found your way to be slightly faster when calling but for higher calls like 1000+ array str_split is faster. – david Sep 16 '11 at 00:08
  • @david what do you use to check speed? – Sami Al-Subhi Feb 13 '12 at 09:35
  • @SamiAl-Subhi i some times use http://stackoverflow.com/questions/2919543/php-codes-performance-test#answer-2919589 and (ab) apache benchmark tool – david Mar 01 '12 at 14:52
2

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);
OZ_
  • 12,492
  • 7
  • 50
  • 68