note 35243 deleted from function.preg-replace by vincent

From: Date: Tue, 26 Aug 2003 09:25:56 +0000
Subject: note 35243 deleted from function.preg-replace by vincent
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-55083@lists.php.net to get a copy of this message
Note Submitter: franz.heinz@gmx.de ---- If you use preg_replace with arrays, like this: <?php $string = "The order of setting the array is very important!"; $pattern=array(); $replacement=array(); $pattern[0] = "/The/"; $pattern[1] = "/order/"; $pattern[2] = "/of/"; $pattern[3] = "/setting/"; $pattern[4] = "/the/"; $pattern[5] = "/array/"; $pattern[6] = "/is/"; $replacement[0] = "tHE"; $replacement[1] = "ORDER"; $replacement[2] = "OF"; $replacement[3] = "SETTING"; $replacement[4] = "THE"; $replacement[5] = "ARRAY"; $replacement[6] = "IS"; echo preg_replace($pattern, $replacement, $string); ?> It gives you your string in the "right order": "tHE ORDER OF SETTING THE ARRAY IS very important!" But if you define your array in an other order, for example like this: <?php $string = "The order of setting the array is very important!"; $pattern=array(); $replacement=array(); $pattern[1] = "/order/"; $pattern[2] = "/of/"; $pattern[0] = "/The/"; $pattern[3] = "/setting/"; $pattern[4] = "/the/"; $pattern[5] = "/array/"; $pattern[6] = "/is/"; $replacement[5] = "ARRAY"; $replacement[1] = "ORDER"; $replacement[2] = "OF"; $replacement[0] = "tHE"; $replacement[3] = "SETTING"; $replacement[4] = "THE"; $replacement[6] = "IS"; echo preg_replace($pattern, $replacement, $string); ?> The String is given you in the "false order": "OF ARRAY ORDER tHE SETTING THE IS very important!" It seems, that preg_replace uses the "setting-order" instead of the "index-order" of the array.

« previous php.notes (#55083) next »