PHP string interrupt in characters

PHP has some kind of function for breaking a string into characters or an array.

Example: OVERFLOW 

I need to break the text OVERFLOW int into: OVERFLOW

OR

  array( 0=> 'O', 1=> 'V', 2=> 'E', 3=> 'R', 4=> 'F', 5=> 'L', 6=> 'O', 7=> 'W' ) 

or any other way ..?

+6
source share
7 answers

There is a function for this: str_split

 $broken = str_split("OVERFLOW", 1); 

If your string can contain multibyte characters, instead of preg_split :

 $broken = preg_split('##u', 'OVERFLOW', -1, PREG_SPLIT_NO_EMPTY); 
+6
source

use this function --- str_split ();

This will split the string into an array of characters.

Example:

 $word="overflow"; $split_word=str_split($word); 
+6
source

Try it....

 $var = "OVERFLOW"; echo $var[0]; // Will print "O". echo $var[1]; // Will print "V". 
+3
source

Use str_split

 $str = "OVERFLOW" ; $var = str_split($str, 1); var_dump($var); 

Exit

 array 0 => string 'O' (length=1) 1 => string 'V' (length=1) 2 => string 'E' (length=1) 3 => string 'R' (length=1) 4 => string 'F' (length=1) 5 => string 'L' (length=1) 6 => string 'O' (length=1) 7 => string 'W' (length=1) 

Example

+2
source

You can do:

 $string = "your string"; $charArray = str_split($string); 
+2
source

Take a look at str_split

You can use it as follows:

 array str_split ( string $string [, int $split_length = 1 ] ); 
+2
source

In truth, he's already broken. So this will work:

 $string = 'Hello I am the string.'; echo $string[0]; // 'H' 

If you specifically want to break it, you can do this:

 $string = 'Hello I am the string.'; $stringarr = str_split($string); 

It depends if you really need to break it or not.

+1
source

Source: https://habr.com/ru/post/924522/


All Articles