Re: PHP 4.0 Bug #9029: preg_split inconsistency
| From: | Wico de Leeuw | Date: | Wed, 31 Jan 2001 13:35:47 +0000 |
| Subject: | Re: PHP 4.0 Bug #9029: preg_split inconsistency | ||
| References: | 1 | Groups: | php.dev |
| Request: | Send a blank email to php-dev+get-45449@lists.php.net to get a copy of this message | ||
you gotta escape the escapes
$b = preg_split('/([\\(\\)])/', $a);
Greetz,
Wico
At 13:13 31-1-01 +0000, anil@recoil.org wrote:
From: anil@recoil.orgOperating system: OpenBSD 2.8PHP version: 4.0.4pl1 PHP Bug Type: PCRE relatedBug description: preg_split inconsistency While trying to write a simple Scheme parser in PHP, we encountered this problem: In perl, you can capture the delimiters passed to split() by enclosing the portion with brackets, in the regexp. This does not work in PHP. For example, here is the perl script, and its output: #!/usr/bin/perl use Data::Dumper; $_ = '(define heshe (if (equal gender "Male") "He" "She")) '; @b = split /([\(\)])/; print Dumper(@b); Output: $VAR1 = ''; $VAR2 = '('; $VAR3 = 'define heshe '; $VAR4 = '('; $VAR5 = 'if '; $VAR6 = '('; $VAR7 = 'equal gender "Male"'; $VAR8 = ')'; $VAR9 = ' "He" "She"'; $VAR10 = ')'; $VAR11 = ''; $VAR12 = ')'; $VAR13 = ' '; But in PHP: <? $a = '(define heshe (if (equal gender "Male") "He" "She")) '; $b = preg_split('/([\(\)])/', $a); var_dump($b); ?> Outputs: array(7) { [0]=> string(0) "" [1]=> string(13) "define heshe " [2]=> string(3) "if " [3]=> string(19) "equal gender "Male"" [4]=> string(11) " "He" "She"" [5]=> string(0) "" [6]=> string(1) " " } The brackets aren't captured, which makes the Scheme parsing a bit difficult :-) There are other ways to do this, of course, but the preg_split() would be the simplest and the most efficient way to do it, as far as we can see. I noticed that the PCRE libraries are only at version 3.0 in PHP, and that 3.4 is available from the main site. I'll try updating to that, and see if it solves this. -- Edit Bug report at: http://bugs.php.net/?id=9029&edit=1 -- PHP Development Mailing List <http://www.php.net/> To unsubscribe, e-mail: php-dev-unsubscribe@lists.php.net For additional commands, e-mail: php-dev-help@lists.php.net To contact the list administrators, e-mail: php-list-admin@lists.php.net