note 96316 added to function.preg-replace

From: Date: Fri, 19 Feb 2010 23:34:46 +0000
Subject: note 96316 added to function.preg-replace
Groups: php.notes 
Request: Send a blank email to php-notes+get-166201@lists.php.net to get a copy of this message
I like using canned SQL queries, where specific values and/or field names are separate in some array. However the %s place-holders of 'printf()' were tedious and error prone because of sequence dependency. By using 'tag' replacement ( a word having some delimiter(s) around it ) and an associative array, I was able to disconnect the canned query from the data in the array. ex: old style: $sql = "SELECT %s FROM %s WHERE %s = %s"; printf($sql, data["my_field"], data["table_name"], data["my_field"], data["field_value"] ); new style: $sql = "SELECT <my_field> FROM <table_name> FROM <my_field> = <field_value> "; $outsql = preg_replace("/<(\w\+)>/e","$data[\"$1\"]", $sql ); NOTE the preg_replace could be optimized to use an annonymous function instead of a "/e" eval of the parm2 expression. Take this a step further... Setup your associative array to contain the output from some previous SQL, and the canned SQLs to reference the fields of other queries. You can then chain your queries where the results of one feeds another. Hope this helps someone. -- jj -- ---- Server IP: 192.41.40.227 Probable Submitter: 63.145.80.194 ---- Manual Page -- http://www.php.net/manual/en/function.preg-replace.php Edit -- https://master.php.net/note/edit/96316 Del: integrated -- https://master.php.net/note/delete/96316/integrated Del: useless -- https://master.php.net/note/delete/96316/useless Del: bad code -- https://master.php.net/note/delete/96316/bad+code Del: spam -- https://master.php.net/note/delete/96316/spam Del: non-english -- https://master.php.net/note/delete/96316/non-english Del: in docs -- https://master.php.net/note/delete/96316/in+docs Del: other reasons-- https://master.php.net/note/delete/96316 Reject -- https://master.php.net/note/reject/96316 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#166201) next »