473,385 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 37508
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: herrcho | last post by:
i'm in the course of learning C, and found these two words "string, string literal" confusing me.. I'd like to know the difference between them.. Thank you
14
by: Charles L | last post by:
I don't know if this is a stupid quesiton or not. I would like to know how to convert an array of characters generated from a previous operation to a string ie how do I append a null character at...
51
by: Alan | last post by:
hi all, I want to define a constant length string, say 4 then in a function at some time, I want to set the string to a constant value, say a below is my code but it fails what is the correct...
17
by: Chad Myers | last post by:
I've been perf testing an application of mine and I've noticed that there are a lot (and I mean A LOT -- megabytes and megabytes of 'em) System.String instances being created. I've done some...
18
by: Ger | last post by:
I have not been able to find a simple, straight forward Unicode to ASCII string conversion function in VB.Net. Is that because such a function does not exists or do I overlook it? I found...
18
by: John | last post by:
Hi, I'm a beginner is using C# and .net. I have big legacy files that stores various values (ints, bytes, strings) and want to read them into a C# programme so that I can store them in a...
20
by: dmurray14 | last post by:
Hey guys, I'm a C++ newbie here - I've messed with VB, but I mostly stick to web languages, so I find C++ to be very confusing at times. Basically, I am trying to import a text file, but I want...
14
by: Shhnwz.a | last post by:
Hi, I am in confusion regarding jargons. When it is technically correct to say.. String or Character Array.in c. just give me your perspectives in this issue. Thanx in Advance.
7
by: jeddiki | last post by:
Hi, I am using a function called htmlwrap() which states that it does NOT add a "<br>" to the 70 character line so that it forces a line wrap. ( the script safely wraps long words without...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.