Jump to content

Archived

This topic is now archived and is closed to further replies.

php_joe

Explode a word

Recommended Posts

I want to break up a word into it's individual letters so that I can stick things between them, reverse them, whatever.

I knocked up this bit of code:
[code]$word = "Mary had a little lamb";
$wordcount = strlen($word);
$i = "0";
while($i < $wordcount){
$key = "$i";
echo "$word[$key]";
$i = ($i + 1);
}[/code]
which works, but I'm sure that there's a better way to do it. Is there a way to explode the characters in the text or something?

Thanks,

Joe

Share this post


Link to post
Share on other sites
[code]
<pre>
<?php

$string = 'Mary had a little lamb';
$pieces = preg_split('//', $string, -1, PREG_SPLIT_NO_EMPTY);
print_r($pieces);

?>
</pre>
[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.