[PHP] split behaviour differences in perl and php
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. IN PERL == [EMAIL PROTECTED] ~]$ cat s1.pl @t = split(/,/ , a,b); $len = $#t + 1; print $len\n; @t = split(/,/, a,); $len = $#t + 1; print $len\n; [EMAIL PROTECTED] sql]$ perl s1.pl 2 1 IN PHP === [EMAIL PROTECTED] ~]$ cat s1.php ?php print count(split(',', 'a,b')).\n; print count(split(',', 'a,')).\n; ? [EMAIL PROTECTED] sql]$ php -q s1.php 2 2 -- Sandip Bhattacharya*Puroga Technologies * [EMAIL PROTECTED] Work: http://www.puroga.com* Home: http://www.sandipb.net PGP/GPG Signature: 51A4 6C57 4BC6 8C82 6A65 AE78 B1A1 2280 A129 0FF3 BLISS is ignorance. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] split behaviour differences in perl and php
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 === [EMAIL PROTECTED] ~]$ cat s1.php ?php print count(split(',', 'a,b')).\n; print count(split(',', 'a,')).\n; ? [EMAIL PROTECTED] 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: [EMAIL PROTECTED] 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; ? [EMAIL PROTECTED] 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 :) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] split behaviour differences in perl and php
* Thus wrote Sandip Bhattacharya: 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. IN PERL == [EMAIL PROTECTED] ~]$ cat s1.pl @t = split(/,/ , a,b); $len = $#t + 1; print $len\n; @t = split(/,/, a,); $len = $#t + 1; print $len\n; [EMAIL PROTECTED] sql]$ perl s1.pl 2 1 IN PHP === [EMAIL PROTECTED] ~]$ cat s1.php ?php print count(split(',', 'a,b')).\n; print count(split(',', 'a,')).\n; ? [EMAIL PROTECTED] sql]$ php -q s1.php 2 2 split in php isn't the same as perl's split, there is preg_split() which you can use: $results = preg_split('/,/','a,', -1, PREG_SPLIT_NO_EMPTY); print(count($results)); // outputs: 1 Curt -- The above comments may offend you. flame at will. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] split behaviour differences in perl and php
PHP's string manipulation functions (such as split()) are not guaranteed to behave exactly like in PERL. The functions that are prefixed by 'preg_' are (PERL REG (EX)) guaranteed to some extent and well documented where preg_ functions are not PERL compliant. So, its not a bug, its just PHP being its own language. Cheers, Andrew Martinez RubyBay Inc. -Original Message- From: Curt Zirzow [mailto:[EMAIL PROTECTED] Sent: Thursday, September 16, 2004 10:45 AM To: [EMAIL PROTECTED] Subject: Re: [PHP] split behaviour differences in perl and php * Thus wrote Sandip Bhattacharya: 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. IN PERL == [EMAIL PROTECTED] ~]$ cat s1.pl @t = split(/,/ , a,b); $len = $#t + 1; print $len\n; @t = split(/,/, a,); $len = $#t + 1; print $len\n; [EMAIL PROTECTED] sql]$ perl s1.pl 2 1 IN PHP === [EMAIL PROTECTED] ~]$ cat s1.php ?php print count(split(',', 'a,b')).\n; print count(split(',', 'a,')).\n; ? [EMAIL PROTECTED] sql]$ php -q s1.php 2 2 split in php isn't the same as perl's split, there is preg_split() which you can use: $results = preg_split('/,/','a,', -1, PREG_SPLIT_NO_EMPTY); print(count($results)); // outputs: 1 Curt -- The above comments may offend you. flame at will. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] split behaviour differences in perl and php
* Thus wrote Sandip Bhattacharya: 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. IN PERL == [EMAIL PROTECTED] ~]$ cat s1.pl @t = split(/,/ , a,b); $len = $#t + 1; print $len\n; @t = split(/,/, a,); $len = $#t + 1; print $len\n; [EMAIL PROTECTED] sql]$ perl s1.pl 2 1 IN PHP === [EMAIL PROTECTED] ~]$ cat s1.php ?php print count(split(',', 'a,b')).\n; print count(split(',', 'a,')).\n; ? [EMAIL PROTECTED] sql]$ php -q s1.php 2 2 split in php isn't the same as perl's split, there is preg_split() which you can use: $results = preg_split('/,/','a,', -1, PREG_SPLIT_NO_EMPTY); print(count($results)); // outputs: 1 Curt Would explode() provide the same technique? $var = a,; $results = explode(,, $var); $results[0] = a; -- --Matthew Sims --http://killermookie.org -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php