Doc #51864 [Bgs->Opn]: preg_replace returns NULL on error

From: Date: Tue, 08 Jun 2010 12:32:16 +0000
Subject: Doc #51864 [Bgs->Opn]: preg_replace returns NULL on error
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-4516@lists.php.net to get a copy of this message
Edit report at http://bugs.php.net/bug.php?id=51864&edit=1 ID: 51864 Updated by: felipe@php.net Reported by: dscotese at litmocracy dot com Summary: preg_replace returns NULL on error -Status: Bogus +Status: Open Type: Documentation Problem Package: PCRE related Operating System: NA PHP Version: Irrelevant New Comment: Re-open... Surely it must be documented. Previous Comments: ------------------------------------------------------------------------ [2010-06-08 14:30:37] felipe@php.net Thank you for taking the time to write to us, but this is not a bug. Please double-check the documentation available at http://www.php.net/manual/ and the instructions on how to report a bug at http://bugs.php.net/how-to-report.php ------------------------------------------------------------------------ [2010-05-20 16:04:05] dscotese at litmocracy dot com Sorry I was not clear in my last post. This is a documentation problem. No test script is necessary. Simply add a reference to preg_last_error to the preg_replacement documentation. However, when you call preg_replace with an array of patterns and replacements, it can fail even though calling it with the corresponding pairs of pattern/replacement one at a time does not fail. I'd consider that a weakness, but not a bug. If it's possible, preg_replace could be rewritten so that when it takes arrays and fails in a way that doesn't indicate any bad patterns, it tries to do the replacements one at a time. But that is not what this bug report is about. I leave it to you to decide if preg_replace should be made more robust in that way. ------------------------------------------------------------------------ [2010-05-20 14:31:28] mike@php.net Thank you for this bug report. To properly diagnose the problem, we need a short but complete example script to be able to reproduce this bug ourselves. A proper reproducing script starts with <?php and ends with ?>, is max. 10-20 lines long and does not require any external resources such as databases, etc. If the script requires a database to demonstrate the issue, please make sure it creates all necessary tables, stored procedures etc. Please avoid embedding huge scripts into the report. ------------------------------------------------------------------------ [2010-05-20 00:45:22] dscotese at litmocracy dot com Description: ------------ Sometimes preg_replace([Array],[Array],$subject) returns null, but the error cannot be reproduced using preg_replace($problem_regex,$repl,$subject). My attempt to reproduce it showed that $problem_regex was not a problem - the replacement worked as designed. I actually removed it from the arrays and did it separately under the assumption that the regex engine might have been biting off more than it could chew. If I shortened $repl, I was able to get the array version to work, but since my $repl was longer, I moved it to a separate call. The docs for preg_replace do not mention preg_last_error. That should be mentioned right where it says it returns NULL on error. Test script: --------------- // You may need to create several more patterns and replacements to get an error // Just make sure they're all legitimate $contents = file_get_contents("textfile.txt"); $find = array('<td><'.'?php readfile("/var/www/www.domain1.com/html/templates/leftmenu2_lfl.txt"); ?'.'></td><td width="663"', '#(?:<table .{450,500})?<\?php\s+readfile[^<>]*?templates/footer(?:lamps)?.txt[\'";) ]*\s*\?'.'>.*#s'); $replace = array('<td>file_get_contents('whatever').'</td><td width="601"', file_get_contents('newFooter.txt')); echo preg_replace($find, $replace, $contents); Expected result: ---------------- When the arrays are large enough and/or the patterns are complicated enough, The output will be empty. The "bug" is that there's no documentation on how to find out why it's empty. Is there a way? ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/bug.php?id=51864&edit=1

« previous php.doc.bugs (#4516) next »