note 97574 modified in function.str-replace by danbrown

From: Date: Mon, 26 Apr 2010 20:28:31 +0000
Subject: note 97574 modified in function.str-replace by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-168678@lists.php.net to get a copy of this message
Might be worth mentioning that a SIMPLE way to accomplish Example 2 (potential gotchas) is to simply start your "replacements" in reverse. So instead of starting from "A" and ending with "E": <?php $search = array('A', 'B', 'C', 'D', 'E'); $replace = array('B', 'C', 'D', 'E', 'F'); // replaces A to B, B to C, C to D, D to E, E to F (makes them all F) // start from "E" and end with "A": $search = array('E', 'D', 'C', 'B', 'A'); $replace = array('F', 'E', 'D', 'C', 'B'); // replaces E to F, D to E, C to D, B to C, A to B (prevents from // multiple replacements of already replaced values) ?> So basically start from the "end" and put the replacements in an order where the "replaced value" won't equal a value that exists later in the "search array". --was-- Might be worth mentioning that a SIMPLE way to accomplish Example 2 (potential gotchas) is to simply start your "replacements" in reverse. So instead of starting from "A" and ending with "E": $search = array('A', 'B', 'C', 'D', 'E'); $replace = array('B', 'C', 'D', 'E', 'F'); // replaces A to B, B to C, C to D, D to E, E to F (makes them all F) start from "E" and end with "A": $search = array('E', 'D', 'C', 'B', 'A'); $replace = array('F', 'E', 'D', 'C', 'B'); // replaces E to F, D to E, C to D, B to C, A to B (prevents from multiple replacements of already replaced values) So basically start from the "end" and put the replacements in an order where the "replaced value" won't equal a value that exists later in the "search array". http://php.net/manual/en/function.str-replace.php

« previous php.notes (#168678) next »