note 32992 modified in function.str-replace by aidan

From: Date: Sun, 27 Jun 2004 13:45:29 +0000
Subject: note 32992 modified in function.str-replace by aidan
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-71891@lists.php.net to get a copy of this message
Having a string for $search and an array for $replace won't work. This is mentioned on the preg_replace() page where it's described as not making sense, but not here. But one could interpret such a situation and hence implement str_replace so that a signature of (string, array, string) or even (string, array, array) would "work". The result of <?php $search = '*'; $replace = range(1,20); $subject = '{*}'; $result = str_replace($search, $replace, $subject); ?> could be an array of elements "{1}", "{2}", "{3}" .... In other words it would have the same effect as <?php $search = '*'; $replace = range(1,20); $subject = '{*}'; $result = array(); foreach($replace as $r) { $result[] = str_replace($search, $r, $subject); } ?> I leave more elaborate applications to your imagination :) The result of a str_replace(string,array,array) would therefore presumably be an array of arrays, with its dimensions indexed by the two arrays passed in. But then there's the question of which is the first dimension and which is the second. --was-- Having a string for $search and an array for $replace won't work. This is mentioned on the preg_replace() page where it's described as not making sense, but not here. But one could interpret such a situation and hence implement str_replace so that a signature of (string, array, string) or even (string, array, array) would "work". The result of $search = '*'; $replace = range(1,20); $subject = '{*}'; $result = str_replace($search, $replace, $subject); could be an array of elements "{1}", "{2}", "{3}" .... In other words it would have the same effect as $search = '*'; $replace = range(1,20); $subject = '{*}'; $result = array(); foreach($replace as $r) { $result[] = str_replace($search, $r, $subject); } I leave more elaborate applications to your imagination :) The result of a str_replace(string,array,array) would therefore presumably be an array of arrays, with its dimensions indexed by the two arrays passed in. But then there's the question of which is the first dimension and which is the second. http://php.net/manual/en/function.str-replace.php

« previous php.notes (#71891) next »