Forum Moderators: coopster

Message Too Old, No Replies

split string into characters

explode?

         

WhosAWhata

3:25 am on Apr 4, 2004 (gmt 0)

10+ Year Member



how exactly do you split a string into individual chars?
ex:
$text = "hello how are you?";
$letters = explode('magic_character',$text);
// $letters = array('h','e','l','l','o',' ','h','o','w',' ','a','r','e',' ','y','o','u','?')

i think it uses explode, but i'm not sure

moltar

4:13 am on Apr 4, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In Perl magic character would be so magic that there you cannot even see it :) In other words, try no character at all. -> ''

WhosAWhata

4:17 am on Apr 4, 2004 (gmt 0)

10+ Year Member



thanks for the suggestion (and the wit) but it returns
Warning: explode(): Empty delimiter. in /home/user/www/www/split.php on line 3

any other ideas?

moltar

4:27 am on Apr 4, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is what you are after: preg_split [ca3.php.net]. Look at example #2.

WhosAWhata

4:29 am on Apr 4, 2004 (gmt 0)

10+ Year Member



looks good, thanks

coopster

12:23 pm on Apr 4, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



str_split [php.net]

mykel79

6:36 pm on Apr 4, 2004 (gmt 0)

10+ Year Member



Or, if you just need to access a specific character, you can do this:
$text="Hello";
echo $text[4];

//prints o

httpwebwitch

1:03 am on Apr 29, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



for($i=0;$<strlen($text);$i++){
$letterarray[]=$text[$i];
}