Hi all,

I'm trying to simplify some string management code. Instead of going through various fields from the form (but not each and all of them) that I'd like to first lowercase all, then upper case words one by one, I was trying to accomplish this by using an array, but I'm not able to get the var to take the new value.

//have now, but would like to simplify:
$var1 = ucwords(strtolower($var1));
...
$var12 = ucwords(strtolower($var12));
$ucWordsArray = array ($var1,$var2,$var3,...,$var12);

$counter = 0;
foreach ($ucWordsArray as $uWA) {
    $uWA = ucwords(strtolower($uWA));
    $counter++;
    $ucWordsArray[$counter] = $uWA;
}
//echo of $var1 (for ex) yields the same from the form.  not changed. 

How can I reassign the newly formated string back into the respective variable?

Thanks!

    im pretty sure u can just use ucwords()... no need to convert to lower first

      thanks for the suggestion. i took that other suggestion from the php site itself. that simplifies it a bit.

      as for the question of 'pushing' the new formatting string back into the original var...any ideas?

        $ucWordsArray = array ($var1,$var2,$var3,...,$var12);
        
        foreach ($ucWordsArray as $key => $uWA) {
            $ucWordsArray[$key] = ucwords(strtolower($uWA));
        }
        

          i tried that, but when i echo out $var1 after, it's still like the original from the form (i.e. not changed). if i echo out $ucWordsArray[0] (to represent $var1), it's fine, but then I've lost the 'original' var name ($var1 in this case). I could accomplish $ucWordsArray[0] by just doing what i had in the original question. :bemused:

          any other ideas? thanks!

            
            $arr = array ("myvar1" => "HeLLo", 
            			"myvar2" => "hELLO",
            			"myvar3" => "hello",
            			"myvar4" => "HELLO");
            
            print_r($arr);
            
            foreach ($arr as $key => $val) {
                $arr[$key] = ucwords(strtolower($val));
            } 
            
            print_r($arr);
            
            /*
            
            Above code outputs;
            
            Array
            (
                [myvar1] => HeLLo
                [myvar2] => hELLO
                [myvar3] => hello
                [myvar4] => HELLO
            )
            Array
            (
                [myvar1] => Hello
                [myvar2] => Hello
                [myvar3] => Hello
                [myvar4] => Hello
            )
            
            */
            

            but i'm a bit confused about what you want, so i'll make this guess too....

            
            
            $myvar1 = "HeLLo";
            $myvar2 = "hELLO";
            $myvar3 = "hello";
            $myvar4 = "HELLO";
            
            $arr = array (&$myvar1, &$myvar2, &$myvar3, &$myvar4);
            
            echo $myvar1 . "<br/>";
            echo $myvar2 . "<br/>";
            echo $myvar3 . "<br/>";
            echo $myvar4 . "<br/>";
            echo "<br/><br/><br/><br/>";
            
            foreach ($arr as $key => $val) {
                $arr[$key] = ucwords(strtolower($val));
            } 
            
            echo $myvar1 . "<br/>";
            echo $myvar2 . "<br/>";
            echo $myvar3 . "<br/>";
            echo $myvar4 . "<br/>";
            
            
            /*
            
            Above code outputs;
            
            HeLLo
            hELLO
            hello
            HELLO
            
            
            Hello
            Hello
            Hello
            Hello
            */
            

              thanks for the reply again.

              going off your 2nd example in the last post, that is exactly what I was refering too. the case of the $var's remains the same (i.e. not changed). Is there a way to, after going through the foreach loop, to reassign the changed string back into the orignal named variable? that way I can refere to, and work with the original named variables but now they will contain the new ucwords case.

              perhaps it's not possible and I should just stick with:

              $var1 = ucwords(strtolower($var1));
              $var2 = ucwords(strtolower($var2));
              $var3 = ucwords(strtolower($var3));
              $var4 = ucwords(strtolower($var4));
              $var5 = ucwords(strtolower($var5));
              $var6 = ucwords(strtolower($var6));
              ...
              $var12 = ucwords(strtolower($var12));
              

              that way I can retain the original variable names. 😕

                rxsid wrote:

                Is there a way to, after going through the foreach loop, to reassign the changed string back into the orignal named variable?

                My last piece of code does exactly that, look at the code closer, all the echo's echo with the original name.

                  btw, how are you getting these variables are they $_POST?

                    yeah, the echo's are of the original name, however the string hasn't changed. in other words, ucwords wasn't retained after the loop.

                    yes, the variables are by $_POST.

                    thanks.

                    wait...sorry. i need more testing. i'll let you know.

                    thanks!!

                      rxsid wrote:

                      yeah, the echo's are of the original name, however the string hasn't changed.

                      Uuuugh, yes it has. did you even try it?

                        dougal85 wrote:

                        Uuuugh, yes it has. did you even try it?

                        Works for me 🙂

                        If all these variables contain the same sort of data and if they're all to be worked on as a group (in other words, if "var1", "var2", ..., are their real names and they aren't misleading) then I'd put them in an array to start with and keep them there for as long as I need to keep working on them as a group. (Naming the fields "var[1]", "var[2]", ... instead would help in this).

                        Otherwise you might as well keep writing repeating lines like

                        $var1 = ucwords(strtolower($var1));
                        //...
                        

                        Because you'll just be writing repeating lines as per dougal85's code to put references them into an array.

                        Well, you could write

                        $arr = array();
                        foreach(array('myvar1', 'myvar2', 'myvar3', 'myvar4') as $var)
                        	$arr[] = &$$var;
                        

                        But you'd still have to write all of the variable names out by hand. It would be much nicer to be able to skip the whole palaver of piles of individual variables and go

                        $vars = array_map('ucwords', array_map('strtolower', $vars));

                          Yeah, my solution was a bit weird and not that useful, i was trying to determine what exactly he wanted. As i was just being told i was doing it wrong!

                          personally I'd much rather have all the values in a nice array anyway.

                            Got it working now, finally had time to test it.
                            I found that I had my $key $val representatives reversed in the loop. When I changed my naming conv to dougal85's example of using the 'default' $key and $val names...I found the mix up. :rolleyes:

                            Thanks folks for your reply's and help. I do appreciate it. 😃

                              Write a Reply...