Exploding a string...

  • Bogey
  • Bogey
  • Genius
  • User avatar
  • Joined: Jul 14, 2005
  • Posts: 8211
  • Loc: USA
  • Status: Offline

Post February 5th, 2009, 3:21 pm

I want to explode a string into an array where all of the letters are a value to the array...

Let's say I have the following string for example...

$str = "abs12fg";

I want that to become equivalent to

array('a', 'b', 's', '1', '2', 'f', 'g');

I've tried explode('', $str); but that doesn't help... thanks.
"Bring forth therefore fruits meet for repentance:" Matthew 3:8
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post February 5th, 2009, 3:21 pm

  • joebert
  • Sledgehammer
  • Genius
  • No Avatar
  • Joined: Feb 10, 2004
  • Posts: 13455
  • Loc: Florida
  • Status: Offline

Post February 5th, 2009, 3:39 pm

Is this something that you can use a for() loop with instead of a foreach loop ?

If given the character indexes, you could still work on each individual character without the added work for formatting it into a different type of array.

Code: [ Select ]
for($i = 0, $toi = strlen($str); $i < $toi; $i++)
{
   work_with($str[$i]);
}
  1. for($i = 0, $toi = strlen($str); $i < $toi; $i++)
  2. {
  3.    work_with($str[$i]);
  4. }
Strong with this one, the sudo is.
  • Bogey
  • Bogey
  • Genius
  • User avatar
  • Joined: Jul 14, 2005
  • Posts: 8211
  • Loc: USA
  • Status: Offline

Post February 5th, 2009, 3:46 pm

Thanks Joebert... I igreatly appreciate your help :D
"Bring forth therefore fruits meet for repentance:" Matthew 3:8
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Joined: Sep 22, 2003
  • Posts: 6130
  • Loc: Seattle, WA
  • Status: Offline

Post February 5th, 2009, 5:36 pm

Be sure to use [square brackets] and not {curly brackets} when indexing the string, since the curly bracket method is deprecated.
The Beer Monocle. Classy.
  • Bogey
  • Bogey
  • Genius
  • User avatar
  • Joined: Jul 14, 2005
  • Posts: 8211
  • Loc: USA
  • Status: Offline

Post February 5th, 2009, 5:55 pm

Alright... thanks spork for mentioning that... I changed it :)
"Bring forth therefore fruits meet for repentance:" Matthew 3:8
  • joebert
  • Sledgehammer
  • Genius
  • No Avatar
  • Joined: Feb 10, 2004
  • Posts: 13455
  • Loc: Florida
  • Status: Offline

Post February 5th, 2009, 6:36 pm

I could have sworn it was the square brackets that were being depreciated for strings in PHP 6. But sure enough, both the PHP manual and posts I've made in the past, say otherwise. :scratchhead:
Strong with this one, the sudo is.
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Joined: Sep 22, 2003
  • Posts: 6130
  • Loc: Seattle, WA
  • Status: Offline

Post February 6th, 2009, 12:16 am

I rock 8)
The Beer Monocle. Classy.
  • dheerhot
  • Novice
  • Novice
  • User avatar
  • Joined: Jan 11, 2009
  • Posts: 34
  • Status: Offline

Post February 6th, 2009, 8:25 am

try this
preg_split('//',$str);

i m not sure tht it will work..
  • Bogey
  • Bogey
  • Genius
  • User avatar
  • Joined: Jul 14, 2005
  • Posts: 8211
  • Loc: USA
  • Status: Offline

Post February 6th, 2009, 2:54 pm

This post associated with This post gave me the solution I wanted... thanks though dheerhot :)
"Bring forth therefore fruits meet for repentance:" Matthew 3:8
  • dheerhot
  • Novice
  • Novice
  • User avatar
  • Joined: Jan 11, 2009
  • Posts: 34
  • Status: Offline

Post February 7th, 2009, 1:34 am

try this

Code: [ Select ]
$str="abcdef";
$arr=str_split($str);
  1. $str="abcdef";
  2. $arr=str_split($str);


u will get output as

$arr[0]='a'
$arr[1]='b'

& so on...
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Joined: Sep 22, 2003
  • Posts: 6130
  • Loc: Seattle, WA
  • Status: Offline

Post February 7th, 2009, 10:01 am

Strings are already indexable by letter, there's no need to create a separate array.
The Beer Monocle. Classy.
  • Bogey
  • Bogey
  • Genius
  • User avatar
  • Joined: Jul 14, 2005
  • Posts: 8211
  • Loc: USA
  • Status: Offline

Post February 7th, 2009, 10:07 am

spork wrote:
Strings are already indexable by letter, there's no need to create a separate array.

They are? How?

The reason I need it as an array is so I can use in_array to check if a certain variable exists, but I guess I could use strpos for that... oh well :lol: thanks.
"Bring forth therefore fruits meet for repentance:" Matthew 3:8
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Joined: Sep 22, 2003
  • Posts: 6130
  • Loc: Seattle, WA
  • Status: Offline

Post February 7th, 2009, 11:41 am

Bogey wrote:
They are? How?

The exact way Joe showed you.
The Beer Monocle. Classy.
  • Bogey
  • Bogey
  • Genius
  • User avatar
  • Joined: Jul 14, 2005
  • Posts: 8211
  • Loc: USA
  • Status: Offline

Post February 7th, 2009, 3:45 pm

:lol: I figured out what you meant after I wrote that and I was too lazy to erase it...
"Bring forth therefore fruits meet for repentance:" Matthew 3:8

Post Information

  • Total Posts in this topic: 14 posts
  • Users browsing this forum: No registered users and 253 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 2011 Unmelted, LLC. Ozzu® is a registered trademark of Unmelted, LLC.