Page 1 of 1

Explode question: second piece when string at end of explode

Posted: Wed Jul 15, 2009 8:05 pm
by JAB Creations
I am looking for some clarification about PHP's explode function. If I explode a string with another string that occurs at the end of the string I'm exploding it does create an empty second piece...

Code: Select all

<?php
$i_r_exploder = explode("world","hello world");
?>
...I'm curious if this is intentional or simply something that has been overlooked in PHP that might be "ironed out" in PHP 6 or 7 (in which case I'd prefer to find something that wouldn't be considered a "fluke"). It would be more convenient that it's intentional though I'd like to cover my bases just in case. Thought please?

Re: Explode question: second piece when string at end of explode

Posted: Wed Jul 15, 2009 8:41 pm
by Skara
My thought would be that it's intentional. "" is still something. Since all strings end in a termination character \0, I can see the logic.
Typed out...

Code: Select all

explode('b',"abab\0");
//so
a, a, \0
//which, since these are still strings, becomes
a\0, a\0, \0
Correct me someone if I'm wrong. I'm basing this off of what I know of strings in C, since php isn't typed or strict about strings.

Re: Explode question: second piece when string at end of explode

Posted: Wed Jul 15, 2009 8:52 pm
by JAB Creations
Thanks...it makes sense though I wanted at least a second opinion. :)