Re: split behaviour differences in perl and php

From: Date: Thu, 16 Sep 2004 11:51:44 +0000
Subject: Re: split behaviour differences in perl and php
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-196847@lists.php.net to get a copy of this message
Sandip Bhattacharya wrote:
This stumped me badly in my present project. Is this a bug or a feature in PHP? I am trying to split a string into two, where only one half (and the delimiter) is present. [ trim ]
IN PHP ======================= [sandip@pluto ~]$ cat s1.php <?php print count(split(',', 'a,b'))."\n"; print count(split(',', 'a,'))."\n"; ?> [sandip@pluto sql]$ php -q s1.php 2 2
If your expand your example slightly, as I did, you will see why you are getting the expected results: burhan@sandbox burhan $ cat s1.php <?php $results = split(',', 'a,b'); print_r($results); print count($results)."\n"; $results = split(',','a,'); print_r($results); print count($results)."\n"; ?> burhan@sandbox burhan $ php -q s1.php Array (
    [0] => a
    [1] => b
) 2 Array (
    [0] => a
    [1] =>
) 2 As you can see, the array contains an empty second element because (correctly) there isn't a second element after the last ,. So, is this a bug? Not from my point of view. Its more bad input, expected result :)

« previous php.general (#196847) next »