Nikki-M Posted February 14, 2008 Share Posted February 14, 2008 Hello All I am new to PHP so please forgive me if this is a "silly" question. I am currently developing a script for my weather based website which takes various weather variables and translates that data into a text based description of the current weather conditions. My question is this: I have a variable loaded with a string something like this: $var1 = "overcast - clouds at 300m - haze" The variable $var1 is dynamic and can change as frequently as every 5 minutes. Is it possible to take each of the words and load them into individual variables, for example: $var2 = "overcast" $var3 = "clouds at 300m" $var4 = "haze" I'm currently running PHP-4.4 on Apache-1.3.41 / FreeBSD-STABLE. if necessary I can upgrade PHP to version 5... Thanks in advance for any help Regards Nikki Quote Link to comment https://forums.phpfreaks.com/topic/91052-php-variables-new-to-php/ Share on other sites More sharing options...
PHP Monkeh Posted February 14, 2008 Share Posted February 14, 2008 $var1 = "overcast - clouds at 300m - haze"; $tmp = explode(" - ", $var1); $var2 = $tmp[0]; // overcast $var3 = $tmp[1]; // clouds at 300m $var4 = $tmp[2]; // haze Quote Link to comment https://forums.phpfreaks.com/topic/91052-php-variables-new-to-php/#findComment-466663 Share on other sites More sharing options...
Nikki-M Posted February 14, 2008 Author Share Posted February 14, 2008 WOW quick responce. Thats exactly what i am looking for. Many thanks Nikki Quote Link to comment https://forums.phpfreaks.com/topic/91052-php-variables-new-to-php/#findComment-466666 Share on other sites More sharing options...
Nikki-M Posted February 14, 2008 Author Share Posted February 14, 2008 $var1 = "overcast - clouds at 300m - haze"; $tmp = explode(" - ", $var1); $var2 = $tmp[0]; // overcast $var3 = $tmp[1]; // clouds at 300m $var4 = $tmp[2]; // haze Hello Again I have just tested this code and works well, however i just have one more question - if you don’t mind. How can i extend this code to keep adding to the variable until the end of "-". You see, sometimes there can be only one or five or more. I'm assuming i would use a do while loop and include the $vars in an array but just not sure of the condition checking format which would look for the "-" and stop when there is no more. Any help would be appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/91052-php-variables-new-to-php/#findComment-466715 Share on other sites More sharing options...
rhodesa Posted February 14, 2008 Share Posted February 14, 2008 Well, after $tmp = explode(" - ", $var1); $tmp is an array with all of the values. depending on what you want to do with it, you can just loop over these values. even better, let's just get rid of $tmp and put it right in a foreach: <?php $var1 = "overcast - clouds at 300m - haze"; foreach(explode(" - ", $var1) as $part){ echo $part; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/91052-php-variables-new-to-php/#findComment-466722 Share on other sites More sharing options...
kenrbnsn Posted February 14, 2008 Share Posted February 14, 2008 Try something like: <?php $var = 'some - text - separated - with - dashes'; $tmp = explode(' - ',$var); $i = 0; foreach ($tmp as $val) { ${'var' . $i} = $val; $i++; } // check to see that it worked for ($j=0;$j<$i;$j++) echo '$var' . $j . ' = ' . ${'var'.$j} . "<br>\n"; ?> But, personally, I would just stick to referencing the values from the array. Ken Quote Link to comment https://forums.phpfreaks.com/topic/91052-php-variables-new-to-php/#findComment-466728 Share on other sites More sharing options...
revraz Posted February 14, 2008 Share Posted February 14, 2008 Are you trying to do something with a ATIS report? Quote Link to comment https://forums.phpfreaks.com/topic/91052-php-variables-new-to-php/#findComment-466742 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.