General Community > Scripting Help

doing an explode() without a separator?

(1/2) > >>

Aquilo:
what I have been trying to do is to take a word and explode it into an array of it's characters - it took my a while but persistence prevailed and found a way to do it but I was wondering if their was a faster way then the for() loop!

so what I'm doing is taking:
$str = 'word';

and turning it into this:
$str = Array
(
    [0] => w
    [1] => o
    [2] => r
    [3] => d
)

this is the for loop I finally got to do it, sorry if this is something very easy in most minds but trying to get this from scratch was kinda frustrating! OR maybe I need sleep!
<?php
for ($i=0; $i < strlen($str); $i++){
   $chr[] = substr("$str", $i, 1);
}
?>

is there already a function to do this in PHP?

[Unknown]:
Umm... that's a bad way :P.  I DEFINITELY have no idea what this "$str" thing is about :P :P.

If it must be an array, this might do the trick.

<?php

$chr = array();
for ($i = 0; $i < strlen($str); $i++)
   $chr[$i] = $str{$i};

?>

But... you know... $blah{} might just do it for you without the array..... you can even do $blah[], but that's deprecated - you should use the curly braces.

-[Unknown]

Aquilo:
after all the time I spent looking for a way to do this and then getting something working I go straight back to php.net and find it right away! :-\ *Kicks self in butt*

$chars = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY);

Aquilo:
hum...  $chr{$i} = $str{$i}; worked to.

what is the difference with brackets and curly braces?

Oh and the string thing is me making a Pythagorean numerology script, like the way to find information about your personality you take your true accepted~parent given birth name converting each letter to it's numerical value then adding them.

Personal Expression = reveals physical and mental constitution
like me : jason sheldon arencibia
would be: 1+1+1+6+5+1+8+5+3+4+6+5+1+9+5+5+3+9+2+9+1+4 = 90 = 9+0 = 9

and then one that counts just vowels and one that counts Consonants >:( too many!
that's why I'm makeing a script for it!!

and another:
Lifes Path:
me: 10/15/1975
would be: 1+0=1 / 1+5=6 / 1+9+7+5=22 = 1+6+22=29 = 2+9=11  :D 11 is a master number you don't mess with it.

Well!! this is two of too many examples used in numerology. :( this is going to take a while but then still these numbers are then interpreted to a meaning.

[Unknown]:
Brackets are for arrays, braces are for strings.

-[Unknown]

Navigation

[0] Message Index

[#] Next page

Go to full version