472,145 Members | 1,431 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,145 software developers and data experts.

break a string into an array of characters

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
7 37445
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
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
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
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
.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
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
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.

Similar topics

14 posts views Thread by Charles L | last post: by
51 posts views Thread by Alan | last post: by
17 posts views Thread by Chad Myers | last post: by
18 posts views Thread by John | last post: by
20 posts views Thread by dmurray14 | last post: by
14 posts views Thread by Shhnwz.a | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.