Possible Duplicate:
PHP preg_split string into letter pairs
I have an string looking like this:
$str = "How are you doing?";
How can I turn this string into an array looking like this:
$arr = array("Ho","w ","ar","r ","yo","u ","do","in","g?");
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.
Answer: You just have to pass (“”) in the regEx section of the Java Split() method. This will split the entire String into individual characters.
Split is used to break a delimited string into substrings. You can use either a character array or a string array to specify zero or more delimiting characters or strings. If no delimiting characters are specified, the string is split at white-space characters.
The split() method splits a string into a list. You can specify the separator, default separator is any whitespace. Note: When maxsplit is specified, the list will contain the specified number of elements plus one.
$array = str_split($str, 2);
Documentation.
Use str_split()
function.
Take a look at str_split(); it allows you to split a string by a definable amount of characters, so your code would look like:
$arr = str_split($str, 2);
Which will split $str
into an array $arr
where each element contains two characters,
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With