note 86690 modified in function.str-replace by felipe

From: Date: Thu, 30 Oct 2008 01:03:18 +0000
Subject: note 86690 modified in function.str-replace by felipe
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-145954@lists.php.net to get a copy of this message
Well, suffering without parameter replace ability like in plsql sql = > "select * from x where id = %1"; execute sql, var1; (sorry for pseudo) here is a function just wrote for my self using str_replace, let me know if there is a better way for such generic 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) { $rep_str = "\$sql = str_replace('%$key','$value',\$sql,\$count);"; eval($rep_str); 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; } } ?> --was-- Well, suffering without parameter replace ability like in plsql sql = > "select * from x where id = %1"; execute sql, var1; (sorry for pseudo) here is a function just wrote for my self using str_replace, let me know if there is a better way for such generic function function sql_prep() { $args = func_get_args(); $sql = array_shift($args); $args_cnt = func_num_args(); $found=0; foreach($args as $key=>$value) { $rep_str = "\$sql = str_replace('%$key','$value',\$sql,\$count);"; eval($rep_str); 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; } } http://php.net/manual/en/function.str-replace.php

« previous php.notes (#145954) next »