| 
        php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login | 
  [2012-03-01 08:25 UTC] 58219758 at qq dot com
 Description:
------------
$search = array('/required/','/min\=(\d+)/','/max\=(\d+)/','/\w+\=([^=]+)/',);
$replace2 = array('R=1','R="$1"','R="$1"',);
It seems doesn't work correctly when replacement contains "="
Test script:
---------------
$search = array('/required/','/min\=(\d+)/','/max\=(\d+)/','/\w+\=([^=]+)/',);
$replace1 = array('R1','R"$1"','R"$1"',);
$replace2 = array('R=1','R="$1"','R="$1"',);
$replace3 = array('R\\=1','R\\="$1"','R\\="$1"',);
$from = 'required min=3 max=5 code=code';
echo preg_replace($search, $replace1, $from);
echo preg_replace($search, $replace2, $from);
echo preg_replace($search, $replace3, $from);
Expected result:
----------------
R1 R"3" R"5"
R=1 R="3" R="5"
R\=1 R\="3" R\="5"
Actual result:
--------------
R1 R"3" R"5"
 ="3" =code
R\=1 R\="3" R\="5"
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits             
             | 
    |||||||||||||||||||||||||||
            
                 
                Copyright © 2001-2025 The PHP GroupAll rights reserved.  | 
        Last updated: Tue Nov 04 15:00:01 2025 UTC | 
This is happening because of the fourth search expression /\w+\=([^=]+)/. It matches the previous replacements and removes them. Without it, this: echo preg_replace( array('/required/', '/min\=(\d+)/', '/max\=(\d+)/'), array('R=1', 'R="$1"', 'R="$1"'), 'required min=3 max=5 code=code' ) . "\n"; Outputs: R=1 R="3" R="5" code=code (As expected.) But the replacements happen one after the other instead of simultaneously, so then it will do the equivalent of this: echo preg_replace( '/\w+\=([^=]+)/', '', 'R=1 R="3" R="5" code=code' ) . "\n"; ..which will match and remove 'R=1 R' and 'R="5" code', producing the "puzzling" output: ="3" =code Here is one way to solve it, by doing all the matches simultaneously with one regexp: echo preg_replace_callback( '/(\w+)(?:=(\S+))?/', function ($parts) { if (!empty($parts[2])) { // have '=' if ($parts[1] == 'min' || $parts[1] == 'max') { return 'R="' . $parts[2] . '"'; } else { return ''; } } else { if ($parts[1] == 'required') { return 'R=1'; } else { return $parts[0]; // unmodified } } }, 'required min=3 max=5 code=code' ) . "\n"; This outputs: R=1 R="3" R="5"