Bug #68310 [Opn->Nab]: preg_split() fail with extended modifier

From: Date: Mon, 27 Oct 2014 15:02:09 +0000
Subject: Bug #68310 [Opn->Nab]: preg_split() fail with extended modifier
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-188323@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=68310&edit=1 ID: 68310 Updated by: nikic@php.net Reported by: david dot proweb at gmail dot com Summary: preg_split() fail with extended modifier -Status: Open +Status: Not a bug Type: Bug Package: PCRE related Operating System: Irrelevant PHP Version: 5.5.18 Block user comment: N Private report: N New Comment: Double-quoted string interpret escape sequences, so what PCRE actually sees are the strings like "/ ? /" (where the first newline is supposed to be an \r and the second one an \r. So you can either escape the backslash (as you did) or use single-quoted strings like '/\r?\n/'. Previous Comments: ------------------------------------------------------------------------ [2014-10-27 13:18:53] david dot proweb at gmail dot com Description: ------------ preg_split(pattern, subject) will fail if I use a pattern with extended (/x) modifier with escaped sequences (like \r or \n), but if I double escape it, it'll work. Seems a bug for me. If I do: /\r?\n/ over "abc\r\n123" it will split to ["abc", "123"]. If I do: /\r?\n/x over "abc\r\n123" it will will fail¹. If I do: /\\r?\\n/x over "abc\r\n123" it will split to ["abc", "123"]. If you repair, I need to escape twice when I use /x modifier. It doesn't occur if I use preg_match(pattern, subject). PHP 4.x it'll works correctly. ¹ Compilation failed: nothing to repeat at offset 1. Test script: --------------- $split_normal = "/\r?\n/"; $split_extended_escaped = "/\r?\n/x"; $split_extended_reescaped = "/\\r?\\n/x"; $split_text = "abc\r\n123"; var_dump(preg_split($split_normal, $split_text)); // OK var_dump(preg_split($split_extended_escaped, $split_text)); // Wrongly FAIL var_dump(preg_split($split_extended_reescaped, $split_text)); // Wrongly OK ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=68310&edit=1

« previous php.bugs (#188323) next »