I wanted to make a simple function that would randomly transform upper and lower case letters from string. I thought i have managed to get it work , but.... it doesn*t and i really have no idea what i got wrong . So can someone please tell me what i did wrong? <?php // big_small_letters function function big_small_letters ( $str ){ $max_number_letters = strlen($str); $current_letter = 0; for ($current_letter = 0 ; $current_letter < $max_number_letters; $current_letter++){ $random = rand(0,1); if( $random = "1" ){ $str{$current_letter} = strtoupper($str{$current_letter}); } else{ $str{$current_letter} = strtolower($str{$current_letter}); } echo "$str" . "----". "$current_letter</br>"; } echo "</br></br>Ended print:"."$str </br>"; echo "Number of signs:"."$max_broj_slova</br>" ; echo "Ending radnom variable:" ."$random</br>"; } big_small_letters ("Some text goes here"); ?> Code (markup):
You are using the string as if it was an array without actually building an array. and your using cury brackets for an array value instead of square brakets. instead try build a new string like <?php // big_small_letters function function big_small_letters ( $str ){ $max_number_letters = strlen($str); $current_letter = 0; $new_str = ""; for ($current_letter = 0 ; $current_letter < $max_number_letters; $current_letter++){ $random = rand(0,1); if( $random = "1" ){ $new_str .= strtoupper(substr($str,$current_letter,$current_letter+1)); } else{ $new_str .= strtolower(substr($str,$current_letter,$current_letter+1)); } } return $new_str; } echo big_small_letters ("Some text goes here"); ?> PHP: havent tested it so dont know if it works, but it gives you a idea
ok scratch that previous on tested this one and it works <?php function big_small_letters ( $str ){ $max_number_letters = strlen($str); $new_str = ""; for ($current_letter = 0 ; $current_letter < $max_number_letters; $current_letter++){ $random = rand(0,1); if( $random == "1" ){ $new_str .= strtoupper(substr($str,$current_letter,1)); }else{ $new_str .= strtolower(substr($str,$current_letter,1)); } } return $new_str; } echo big_small_letters ("Some text goes here"); ?> PHP:
so it does, learn something new everyday, in that case if( $random == "1" ){ $new_str .= strtoupper($str{$current_letter}); }else{ $new_str .= strtolower($str{$current_letter}); } PHP:
Those methods are extremely intensive. This is a much sexier way: function jiggle($string) { for($output = '', $i = 0, $len = strlen($string); $i < $len; $i++) $output .= mt_rand(0,1) ? strtoupper($string{$i}) : strtolower($string{$i}); return $output; } PHP: Edit: Enjoy
You could also prestore the uppercase/lowercase values, and avoid the multiple function calls. As well, if you start with the string already set to uppercase (or lowercase), you only have to copy over about half the time: function big_small_letters($string) { $uc=strtoupper($string); $result=strtolower($string); $i=strlen($string); while ($i--) { if (mt_rand(0,1)) $result[$i]=$uc[$i]; } return $result; } PHP: Here, you're starting with a lowercase string, and only adding an uppercase character as needed, on a character by character basis.