Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to convert a string to an array in php [duplicate]

Tags:

php

how to convert a string in array in php i.e

$str="this is string"; 

should be like this

  arr[0]=this   arr[1]=is   arr[2]=string  

The str_split($str, 3); splits the string in 3 character word but I need to convert the string after whitespace in an array.

like image 909
hunter Avatar asked Mar 05 '11 12:03

hunter


People also ask

Which function is used to convert string to array?

The str_split() is an inbuilt function in PHP and is used to convert the given string into an array.

What is implode and explode function in PHP?

Definition and UsageThe implode() function returns a string from the elements of an array. Note: The implode() function accept its parameters in either order. However, for consistency with explode(), you should use the documented order of arguments. Note: The separator parameter of implode() is optional.

How do you turn a string into an array in Javascript?

The split() method splits a string into an array of substrings. The split() method returns the new array. The split() method does not change the original string. If (" ") is used as separator, the string is split between words.


1 Answers

Use explode function

$array = explode(' ', $string); 

The first argument is delimiter

like image 159
deceze Avatar answered Sep 25 '22 14:09

deceze