424,054 Members | 1,044 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,054 IT Pros & Developers. It's quick & easy.

break a string into an array of characters

P: n/a
I'm a little bit amazed that Google wasn't able to find me previous
posts on this subject, as I'm sure it's been covered many times before,
but it's possible I'm using all the wrong keywords.

If I want to break a string into an array of individual characters, how
do I do it? I thought explode with an empty delimiter might work, but
it didn't.

Jul 17 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
You need to use preg_split

like this

$str ="blabla and some more blah";
$char_buff = preg_split('//', $str, -1);

print_r($char_buff);
This splits every single character, including whitespaces.
If you want to ommit whitespaces, use preg_split('//', $str, -1,
PREG_SPLIT_NO_EMPTY);
<lk******@geocities.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
I'm a little bit amazed that Google wasn't able to find me previous
posts on this subject, as I'm sure it's been covered many times before,
but it's possible I'm using all the wrong keywords.

If I want to break a string into an array of individual characters, how
do I do it? I thought explode with an empty delimiter might work, but
it didn't.

Jul 17 '05 #2

P: n/a
lk******@geocities.com wrote:
If I want to break a string into an array of individual characters, how
do I do it? I thought explode with an empty delimiter might work, but
it didn't.


A string is already an array of chracters:

$str="bar";

for($i=0;$i<strlen($str);$i++)
{
echo "position $i: [".$str{$i}."]\n";
}

Note the {} instead of [] for strings.
Jul 17 '05 #3

P: n/a
Oh come on! Regexps have their use, but using them to transform a
string into an array of characters is questionable, don't you think?

What about str_split
(http://www.php.net/manual/en/function.str-split.php)? BTW, the
description of the function is "Convert a string to an array". Seems
like a RTFM to me!

Jul 17 '05 #4

P: n/a
Carved in mystic runes upon the very living rock, the last words of of
comp.lang.php make plain:
What about str_split Seems like a RTFM to me!


Indeed, and the FM will tell you that it's PHP5 only, which is still pre-
release.

--
Alan Little
Phorm PHP Form Processor
http://www.phorm.com/
Jul 17 '05 #5

P: n/a
.oO(Alan Little)
What about str_split

Seems like a RTFM to me!


Indeed, and the FM will tell you that it's PHP5 only, which is still pre-
release.


5.0.2 is released for production, 5.0.3 is coming (currently RC).

Micha
Jul 17 '05 #6

P: n/a
Thank you. I found some code I'd written 2 years ago in which I'd
walked through as a string as an array, and then used that to solve my
problems. It is sad to think how many things we learn and then forget.

Jul 17 '05 #7

P: n/a
Carved in mystic runes upon the very living rock, the last words of
Michael Fesser of comp.lang.php make plain:
.oO(Alan Little)
What about str_split

Seems like a RTFM to me!


Indeed, and the FM will tell you that it's PHP5 only, which is still
pre- release.


5.0.2 is released for production, 5.0.3 is coming (currently RC).


Oops! You're right. I guess I should RTFWP more carefully, eh? To be
honest, I haven't paid much attention to PHP5 yet. Heck, the current
release of Phorm still supports PHP3. I have a bug fix release coming out
that will still support 3; the release after that will be a major release
and will only support 4 and 5. It will be a long time before I abandon
PHP4 support, probably about the time PHP6 comes out....

--
Alan Little
Phorm PHP Form Processor
http://www.phorm.com/
Jul 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.