Here's a tough one

Let’s say I have a big group of strings in a varible, seperated by a a new line.

so it would be:

$str = "
word1
word2
word3
word4
word5
";

Now I want it to take all those words and store them in their own seperate variable automatically.

so it would be:

$str1 = “word1”;
$str2 = “word2”;
$str3 = “word3”;
$str4 = “word4”;
$str5 = “word5”;

Think theres a way to do this? Thanks.

Let’s see here cracks knuckles

[php]<?php

$str = "
word1
word2
word3
word4
word5
";

$newstr = explode(“n”, $str);

foreach($newstr as $key => $value){

if($value != “”){

$str{$key} = $value;

}

}

?>
[/php]

Untested, but it should work as you want, each line (excluding the blank lines) should be stored in $str1, $str2, etc.

Hmmmm. it doesnt seem to work. I echoed out $str1, $str2, etc. and nothing came up. Whats the problem?

I’ll try
[php]

<?php $str = " word1 word2 word3 word4 word5 "; $newstr = explode("n", $str); print_r($newstr); ?>

[/php]

oh my god how did you do that with such a simple code? it works perfectly! Thanks a lot.

print_r is used to print out an array.

Reference:
http://www.php.net/manual/en/function.print-r.php

I’d give it a trim() first to remove empty lines.

Sponsor our Newsletter | Privacy Policy | Terms of Service