Explode question: second piece when string at end of explode

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Explode question: second piece when string at end of explode

Post 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?
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

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

Post 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.
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

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

Post by JAB Creations »

Thanks...it makes sense though I wanted at least a second opinion. :)
Post Reply