Jump to content

Recommended Posts

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

 

 

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/91052-php-variables-new-to-php/
Share on other sites

$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.

 

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;
  }
?>

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.