|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Oct 29 05:00:01 2025 UTC |
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 */