note 69594 deleted from function.str-replace by colder

From: Date: Tue, 12 Sep 2006 10:03:52 +0000
Subject: note 69594 deleted from function.str-replace by colder
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-117048@lists.php.net to get a copy of this message
Note Submitter: crazy dot stu at gmail dot com Reason: bad code ---- I've come up with a way of using arrays to replace things in a string. Here it is with an example: <?php // Array STR Replace Function By Nesianstyles // Email: crazy.stu@gmail.com function array_str_replace($array, $str) { if (is_array($array)) { foreach ($array as $key => $value) { $str = str_replace($key, $value, $str); } return $str; } } $examplearray = array( '[time]' => '8:00pm', '[date]' => '30th' ); $string = "The time is [time] and date is [date]"; $string = array_str_replace($examplearray, $string); echo $string; // Will echo "The time is 8:00pm and date is 30th" ?>

« previous php.notes (#117048) next »