Forum Moderators: coopster

Message Too Old, No Replies

issues with explode

working but not right

         

Matthew1980

7:57 pm on Feb 21, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there People of WebmasterWorld,

This code runs ok, simple enough, but not in the places I would expect:-

$user = str_replace(".", "#", "#date.country");
$user_detail = explode("#", $user);

if I: print_r($user_detail);

I get this:-

Array ( [0] => [1] => date [2] => country )

My question is, shouldn't date be in ['0'] and country be in ['1'], or have I missed something really obvious?

Cheers,
MRb

ninenote

9:54 pm on Feb 21, 2010 (gmt 0)

10+ Year Member



The separator is #

So explode would separate something like this

A#B#C

your string like this "#date#country"

So
A -> ""
B -> "date"
C -> "country"

Matthew1980

10:04 pm on Feb 21, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there Ninenote,

As far as I understand it, yes, the # is the separator, and as there is nothing preceding the first one, there should be only two keys of the array populated, 0 & 1, maybe I should strip the first # from the string before I split it into an array, its funny how these things catch us out from time to time ;-)

Cheers,

MRb

dreamcatcher

11:53 am on Feb 22, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Doesn`t matter that there is nothing preceding #. As ninenote mentioned, when you explode the array you have 3 slots, not 2. Data doesn`t matter. For example the following will give you 3 slots regardless of data:

$string = '###';
$array = explode('#',$string);
print_r($array);

array (
0 => '',
1 => '',
2 => ''
)

dc

CyBerAliEn

4:57 pm on Feb 22, 2010 (gmt 0)

10+ Year Member



This is an annoying issue when you experience it! I've been there, done that.

Instead of just removing the first '#' from the string, I would recommend you perform a loop through the array (many ways to do this) and remove any entries that are "null" values.

In my case, when I use explode, I usually don't want or anticipate having empty values (for examples such as '#xcf##fg#ghf'). So I usually have empty values stripped out.

You have to keep in mind the function is doing what it is told and designed to do. It starts an array entry at the beginning of the string. As soon as it sees the "separator", it removes the separator, and moves on to a new array entry. So if there are multiple separators in a row, you'll get multiple array entries with a null value.

Note: You could also get around this by using regular expressions to remove duplicated separator characters.

Matthew1980

6:19 pm on Feb 22, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there Everyone,

Thanks for the input, after reading up on this, I now see what's what. I have just misunderstood how it performed its operation. And as cyberalien says :
You have to keep in mind the function is doing what it is told and designed to do

As I said though, it works fine, I just couldn't understand why I wasn't getting as I was expecting, thanks for the clarification though, much appreciated.

Cheers,

MRb