0

I have a variable, lets say

$var = "name1 field2_name name3 name4";

i need them to be in an array separately like this

$arr = array ('name1', 'field2_name', 'name3', 'name4');

so i can echo words in separate like this

echo $arr[0]; //echoes name1
Vladimir
  • 1,602
  • 2
  • 18
  • 40
  • 2
    Look up [`explode`](http://php.net/manual/en/function.explode.php). An awful name, but it returns "an array of strings, each of which is a substring of string formed by splitting it on boundaries formed by the string delimiter." Most other languages call this operation "split". – user2246674 Jun 15 '13 at 22:23
  • As an alternative to the obvious explode(), you could use str_word_count() with a custom character set to include numbers and underscores – Mark Baker Jun 15 '13 at 22:29

2 Answers2

1

Use explode()

$arr = explode(' ', $var);
Jérôme
  • 2,070
  • 15
  • 21
1

use the php explode function

$pizza  = "piece1 piece2 piece3 piece4 piece5 piece6";
$pieces = explode(" ", $pizza);
echo $pieces[0]; // piece1
echo $pieces[1]; // piece2
Gunr Jesra
  • 110
  • 1
  • 10