exploding with an empty separator

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
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

exploding with an empty separator

Post by anjanesh »

explode() doesn't accept an empty separator. Is there a workaround for this ?

Code: Select all

$arr = array();
$str = "12345";
for ($i=0; $i<strlen($str); $i++)
 {
        $arr[] = $str[$i];
 }
print_r($arr);
Thanks
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

str_split(), but you can access a string as an array of bytes already, so what does it matter? :?
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

what's wrong wth that? or you are looking for an alternate/built-in solution?
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

Ah...PHP 5's str_split ! Thanks.
but you can access a string as an array of bytes already, so what does it matter?
I want to use a number of array specific functions like array_unique etc on the string.
what's wrong wth that? or you are looking for an alternate/built-in solution?
With so many functions like explode, split etc I thought there already is some built-in one. Just didn't check for PHP5-only ones.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

anjanesh wrote:Ah...PHP 5's str_split ! Thanks.
but you can access a string as an array of bytes already, so what does it matter?
I want to use a number of array specific functions like array_unique etc on the string.
what's wrong wth that? or you are looking for an alternate/built-in solution?
With so many functions like explode, split etc I thought there already is some built-in one. Just didn't check for PHP5-only ones.

Code: Select all

$str = 'foobar123';

$letters = array();
for ($i = 0, $len = strlen($str); $i < $len; $i++)
{
    $letters[] = $str[$i];
}
Post Reply