]> granicus.if.org Git - php/commitdiff
- Fixed bug #50579 (RegexIterator::REPLACE doesn't work)
authorFelipe Pena <felipe@php.net>
Sat, 6 Nov 2010 00:09:50 +0000 (00:09 +0000)
committerFelipe Pena <felipe@php.net>
Sat, 6 Nov 2010 00:09:50 +0000 (00:09 +0000)
NEWS
ext/spl/spl_iterators.c
ext/spl/tests/bug50579.phpt [new file with mode: 0644]

diff --git a/NEWS b/NEWS
index c9098d7a75561910ae9586077bc809c4096906b9..0bce8965c71dcb80be650d49ee18c5273520d1ef 100644 (file)
--- a/NEWS
+++ b/NEWS
   (a_jelly_doughnut at phpbb dot com, Pierre)
 - Fixed bug #50953 (socket will not connect to IPv4 address when the host has
   both IPv4 and IPv6 addresses, on Windows). (Gustavo, Pierre)
+- Fixed bug #50579 (RegexIterator::REPLACE doesn't work). (Felipe)
 - Fixed bug #50524 (proc_open on Windows does not respect cwd as it does on
   other platforms). (Pierre)
 - Fixed bug #50345 (nanosleep not detected properly on some solaris versions).
index 64d783db0fda787a7c8b0648a98fecc331d79ede..dc6f17bac0100d39bb43c2cfab695ff18b0da608 100755 (executable)
@@ -1733,7 +1733,7 @@ SPL_METHOD(RegexIterator, accept)
 {
        spl_dual_it_object *intern = (spl_dual_it_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
        char       *subject, tmp[32], *result;
-       int        subject_len, use_copy, count, result_len;
+       int        subject_len, use_copy, count = 0, result_len;
        zval       subject_copy, zcount, *replacement;
        
        if (intern->current.data == NULL) {
@@ -1796,8 +1796,8 @@ SPL_METHOD(RegexIterator, accept)
                break;
 
        case REGIT_MODE_REPLACE:
-               replacement = zend_read_property(intern->std.ce, getThis(), "replacement", sizeof("replacement")-1, 1 TSRMLS_CC);
-               result = php_pcre_replace_impl(intern->u.regex.pce, subject, subject_len, replacement, 0, &result_len, 0, NULL TSRMLS_CC);
+               replacement = zend_read_property(intern->std.ce, intern->inner.zobject, "replacement", sizeof("replacement")-1, 1 TSRMLS_CC);
+               result = php_pcre_replace_impl(intern->u.regex.pce, subject, subject_len, replacement, 0, &result_len, -1, &count TSRMLS_CC);
                
                if (intern->u.regex.flags & REGIT_USE_KEY) {
                        if (intern->current.key_type != HASH_KEY_IS_LONG) {
@@ -1811,6 +1811,7 @@ SPL_METHOD(RegexIterator, accept)
                        MAKE_STD_ZVAL(intern->current.data);
                        ZVAL_STRINGL(intern->current.data, result, result_len, 0);
                }
+               RETVAL_BOOL(count > 0);
        }
 
        if (intern->u.regex.flags & REGIT_INVERTED) {
diff --git a/ext/spl/tests/bug50579.phpt b/ext/spl/tests/bug50579.phpt
new file mode 100644 (file)
index 0000000..3045228
--- /dev/null
@@ -0,0 +1,40 @@
+--TEST--
+Bug #50579 (RegexIterator::REPLACE doesn't work)
+--FILE--
+<?php
+
+class foo extends ArrayIterator {
+       public function __construct( ) {
+               parent::__construct(array(
+               'test1'=>'test888', 
+               'test2'=>'what?', 
+               'test3'=>'test999'));
+               $this->replacement = '[$1]';
+       }
+}
+$h = new foo;
+$i = new RegexIterator($h, '/^test(.*)/', RegexIterator::REPLACE);
+$h->replacement = '[$0]';
+foreach ($i as $name=>$value) {
+       echo $name . '=>' . $value . "\n";
+}
+  
+$h->replacement = '$1';
+foreach ($i as $name=>$value) {
+       echo $name . '=>' . $value . "\n";
+}
+
+$h = new foo;
+$i = new RegexIterator($h, '/^test(.*)/', RegexIterator::REPLACE);
+foreach ($i as $name=>$value) {
+       echo $name . '=>' . $value . "\n";
+}
+
+?>
+--EXPECTF--
+test1=>[test888]
+test3=>[test999]
+test1=>888
+test3=>999
+test1=>[888]
+test3=>[999]