note 110136 deleted from function.preg-replace-callback by aharvey
| From: | aharvey@php.net | Date: | Mon, 24 Sep 2012 01:25:53 +0000 |
| Subject: | note 110136 deleted from function.preg-replace-callback by aharvey | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-190980@lists.php.net to get a copy of this message | ||
Note Submitter: qiuty at mail dot ru
----
It seems that preg_match_all + str_repalce is more flexible than preg_replace_callback. Here is a
function to replace placeholders in SQL statement by corresponding array values
<?php
function parsef($pattern, $Repl, $query) {
if(preg_match_all($pattern, $query, $P, PREG_SET_ORDER)) {
foreach($P as $p) {
$query = str_replace($p[0], $Repl[$p[1]], $query);
}
}
return $query;
}
// How to use
$Data = array('code' => 1, 'name' => 'Pit');
$query = "SELECT DISTINCT code, name FROM table_name WHERE code='{code}' AND
name='{name}'";
$query = parsef('/\\{(.+?)\\}/', $_Data, $query);
?>