php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #6346 preg_replace with arrays truncates results
Submitted: 2000-08-25 04:43 UTC Modified: 2000-08-25 09:52 UTC
From: rick at eastcore dot net Assigned:
Status: Closed Package: Misbehaving function
PHP Version: 4.0.1pl2 OS: Linux (2.2.16)
Private report: No CVE-ID: None
 [2000-08-25 04:43 UTC] rick at eastcore dot net
I noticed that a system which worked fine with PHP3 
broke under PHP4.  In tracking down the problem I found
that preg_replace was the source of the difficulty.

The following simple PHP script truncates the returned
string:

<?
    $text = "this is some test textthis is some test textthis is some test textthis is some test textthis is some test textthis is some
test textthis is some test textthis is some test textthis is some test textthis is some test textthis is some test textthis is some test
 textthis is some test textthis is some test textthis is some test textthis is some test textthis is some test textthis is some test tex
tthis is some test textthis is some test textthis is some test textthis is some test textthis is some test textthis is some test textthi
s is some test textthis is some test textthis is some test textthis is some test textthis is some test textthis is some test textthis is
 some test textthis is some test textthis is some test textthis is some test textthis is some test text\n";
    $array1 = array();
    $array2 = array();
    $text2 = preg_replace($array1, $array2, $text);
    print "text = $text2<br>\n";
?>  

It appears that using empty array(s) in a preg_replace
truncates the string returned -- which differs significantly
from the no-op behavior in php3.

The configure command is:

configure --with-mysql=/usr --enable-versioning --with-apache=../apache_1.3.12


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2000-08-25 05:16 UTC] rick at eastcore dot net
Here's an attempt at a patch to solve the problem.  The following caveats should be seriously heeded:
I don't have a system handy to compile and test the patch, but I should tomorrow.
I have about 15 minutes worth of familiarity with the PHP4
source base.

Given that, if the patch doesn't work it should at least point to what appears to be the right place in the code to apply a fix (the problem is that the length of the arrays never appears to be checked and the replacement functions don't seem to deal well with empty arrays).

--- php-4.0.1.pl2.orig/ext/pcre/php_pcre.c      Mon Jun 12 14:55:57 2000
+++ php-4.0.1pl2/ext/pcre/php_pcre.c    Fri Aug 25 04:03:47 2000
@@ -779,11 +779,15 @@
                /* Duplicate subject string for repeated replacement */
                subject_value = estrndup((*subject)->value.str.val, (*subject)->value.str.len);
                subject_len = (*subject)->value.str.len;
-
+
+               // empty regex array should leave subject untouched
+               if (zend_hash_num_elements(regex->value.ht) == 0) return subject_value;
                zend_hash_internal_pointer_reset(regex->value.ht);
                if (replace->type == IS_ARRAY) {
-                       zend_hash_internal_pointer_reset(replace->value.ht);
+                   // empty replace array should leave subject untouched
+                   if (zend_hash_num_elements(replace->value.ht) == 0) return subject_value;
+                   zend_hash_internal_pointer_reset(replace->value.ht);
                }
                else {
                        /* Set replacement value to the passed one */

 [2000-08-25 09:52 UTC] andrei@php.net
Fixed in CVS, thanks.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon May 06 18:01:35 2024 UTC