note 87325 deleted from function.str-replace by danbrown

From: Date: Sun, 30 Nov 2008 18:36:07 +0000
Subject: note 87325 deleted from function.str-replace by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-147447@lists.php.net to get a copy of this message
Note Submitter: DarksidePuffin ---- Do -not- use eval for that...eval has very, very few uses, and str_replace is not one of them. Here is an example of a better version of your function: <?php function sql_prep() { $args = func_get_args(); $sql = array_shift($args); $args_cnt = func_num_args(); $found=0; foreach($args as $key => $value) { $sql = str_replace('%'.$key,$value,$sql,$count); if($count) { $found++; } } if($found == $args_cnt-1) { return $sql; } else { echo "WARNING: number of subs=".($args_cnt-1)." does not match number of reps=$found"; return $sql; } } ?> there are other ways to do it, including using array_walk, array_keys,and str_replace with two arrays and no count variable, but eval is absolutely unnecessary.

« previous php.notes (#147447) next »