Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php break string into characters

Tags:

string

php

In PHP is there any function to break up string in to characters or array.

Example: OVERFLOW

I need to break up the above text OVERFLOW int to: O V E R F L O W

OR

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

or any otherway is there..?

like image 570
AnNaMaLaI Avatar asked Sep 04 '12 10:09

AnNaMaLaI


People also ask

How do I split a character in a string in PHP?

The str_split() is an inbuilt function in PHP and is used to convert the given string into an array. This function basically splits the given string into smaller strings of length specified by the user and stores them in an array and returns the array.

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

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. This functions returns an array containing the strings formed by splitting the original string.

What is split function in PHP?

PHP - Function split() The split() function will divide a string into various elements, the boundaries of each element based on the occurrence of pattern in string.

What is explode 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

There is a function for this: str_split

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

If your string can contain multi byte characters, use preg_split instead:

$broken = preg_split('##u', 'OVERFLOW', -1, PREG_SPLIT_NO_EMPTY);
like image 93
Arnaud Le Blanc Avatar answered Nov 01 '22 19:11

Arnaud Le Blanc