Simple function help

Discussion in 'PHP' started by vfistri1, Jun 25, 2008.

  1. #1
    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 :confused: . 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):
     
    vfistri1, Jun 25, 2008 IP
  2. furnissg

    furnissg Peon

    Messages:
    61
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    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
     
    furnissg, Jun 25, 2008 IP
  3. furnissg

    furnissg Peon

    Messages:
    61
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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:
     
    furnissg, Jun 25, 2008 IP
  4. matthewrobertbell

    matthewrobertbell Peon

    Messages:
    781
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    0
    #4
    $value{1} will actually give the character at position one within the variable $value.
     
    matthewrobertbell, Jun 25, 2008 IP
  5. furnissg

    furnissg Peon

    Messages:
    61
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    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:
     
    furnissg, Jun 25, 2008 IP
  6. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #6
    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 ;)
     
    Danltn, Jun 25, 2008 IP
  7. David Pankhurst

    David Pankhurst Member

    Messages:
    39
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #7
    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.
     
    David Pankhurst, Jun 28, 2008 IP