Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: Split string into array, like explode with no delimiter

I have a string such as:

"0123456789"

And I need to split each character into an array.

I, for the hell of it, tried:

explode('', '123545789'); 

But it gave me the obvious: Warning: No delimiter defined in explode) ..

How would I come across this? I can't see any method off hand, especially just a function.

like image 509
oni-kun Avatar asked Jan 31 '10 02:01

oni-kun


People also ask

How convert string to array in PHP with explode?

1) Convert String to Array using explode()explode() method is one of the built-in function in PHP which can be used to convert string to array. The explode() function splits a string based on the given delimiter. A delimiter acts as a separater and the method splits the string where the delimiter exists.

What is difference between explode () or implode () in PHP?

PHP Explode function breaks a string into an array. PHP Implode function returns a string from an array.

How can I split a string into two parts in PHP?

PHP | explode() Function explode() is a built in function in PHP used to split a string in different strings. The explode() function splits a string based on a string delimiter, i.e. it splits the string wherever the delimiter character occurs.

What does explode () do in PHP?

The explode() function breaks a string into an array. Note: The "separator" parameter cannot be an empty string. Note: This function is binary-safe.


1 Answers

$array = str_split("0123456789bcdfghjkmnpqrstvwxyz"); 

str_split takes an optional 2nd param, the chunk length (default 1), so you can do things like:

$array = str_split("aabbccdd", 2);  // $array[0] = aa // $array[1] = bb // $array[2] = cc  etc ... 

You can also get at parts of your string by treating it as an array:

$string = "hello"; echo $string[1];  // outputs "e" 
like image 77
Erik Avatar answered Oct 11 '22 12:10

Erik