Jump to content


Photo

Explode a word


  • Please log in to reply
2 replies to this topic

#1 php_joe

php_joe
  • Members
  • PipPipPip
  • Advanced Member
  • 175 posts

Posted 22 September 2006 - 03:24 AM

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:
$word = "Mary had a little lamb";
$wordcount = strlen($word);
$i = "0";
while($i < $wordcount){
$key = "$i";
echo "$word[$key]";
$i = ($i + 1);
}
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
[url=http://darkswordsguide.com][/url]

#2 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 22 September 2006 - 03:40 AM

<pre>
<?php

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

?>
</pre>

Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#3 php_joe

php_joe
  • Members
  • PipPipPip
  • Advanced Member
  • 175 posts

Posted 23 September 2006 - 02:19 AM

Thank you very much!
[url=http://darkswordsguide.com][/url]




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users