Exploding a string...

  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8399
  • Loc: USA

Post 3+ Months Ago

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.
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13503
  • Loc: Florida

Post 3+ Months Ago

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. }
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8399
  • Loc: USA

Post 3+ Months Ago

Thanks Joebert... I igreatly appreciate your help :D
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6252
  • Loc: Seattle, WA

Post 3+ Months Ago

Be sure to use [square brackets] and not {curly brackets} when indexing the string, since the curly bracket method is deprecated.
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8399
  • Loc: USA

Post 3+ Months Ago

Alright... thanks spork for mentioning that... I changed it :)
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13503
  • Loc: Florida

Post 3+ Months Ago

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:
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6252
  • Loc: Seattle, WA

Post 3+ Months Ago

I rock 8)
  • dheerhot
  • Novice
  • Novice
  • User avatar
  • Posts: 34

Post 3+ Months Ago

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

i m not sure tht it will work..
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8399
  • Loc: USA

Post 3+ Months Ago

This post associated with This post gave me the solution I wanted... thanks though dheerhot :)
  • dheerhot
  • Novice
  • Novice
  • User avatar
  • Posts: 34

Post 3+ Months Ago

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
  • Posts: 6252
  • Loc: Seattle, WA

Post 3+ Months Ago

Strings are already indexable by letter, there's no need to create a separate array.
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8399
  • Loc: USA

Post 3+ Months Ago

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.
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6252
  • Loc: Seattle, WA

Post 3+ Months Ago

Bogey wrote:
They are? How?

The exact way Joe showed you.
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8399
  • Loc: USA

Post 3+ Months Ago

:lol: I figured out what you meant after I wrote that and I was too lazy to erase it...

Post Information

  • Total Posts in this topic: 14 posts
  • Users browsing this forum: No registered users and 89 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
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.