note 87325 added to function.str-replace

From: Date: Sun, 30 Nov 2008 18:31:51 +0000
Subject: note 87325 added to function.str-replace
Groups: php.notes 
Request: Send a blank email to php-notes+get-147446@lists.php.net to get a copy of this message
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. ---- Server IP: 66.207.199.35 Probable Submitter: 71.7.243.176 ---- Manual Page -- http://www.php.net/manual/en/function.str-replace.php Edit -- https://master.php.net/note/edit/87325 Del: integrated -- https://master.php.net/note/delete/87325/integrated Del: useless -- https://master.php.net/note/delete/87325/useless Del: bad code -- https://master.php.net/note/delete/87325/bad+code Del: spam -- https://master.php.net/note/delete/87325/spam Del: non-english -- https://master.php.net/note/delete/87325/non-english Del: in docs -- https://master.php.net/note/delete/87325/in+docs Del: other reasons-- https://master.php.net/note/delete/87325 Reject -- https://master.php.net/note/reject/87325 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#147446) next »