Here's a tough one


#1

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.


#2

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.


#3

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


#4

I’ll try
[php]

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

[/php]


#5

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


#6

print_r is used to print out an array.

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


#7

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