regex - PHP - Replace exact match preg_replace -
regex - PHP - Replace exact match preg_replace -
i'm trying implement very simple pdo bindvalue() function. (just educational purpose)
so, first in "frontend" code, have sql query :
insert logs values (:referer, :id_cookie, :id_cookie_com);
and code looks :
$stmt->bindvalue(':referer', 'test'); $stmt->bindvalue(':id_cookie', 'test2'); $stmt->bindvalue(':id_cookie_com', 'test3');
i'm splitting query, , i'm getting parameters begins ":" in array, works well.
array ( [0] => :referer [1] => :id_cookie [2] => :id_cookie_com )
but, when seek replace parameters in query, i'm facing little problem.
if :
public function bindparam($parameter, $variable, $type = null) { $this->sql = preg_replace('/'.$parameter.'/', $variable, $this->sql); }
i'm getting result :
insert logs values (test, test2, test3_com)
i can't manage remove _com @ end of query. know why it's here, it's because preg_replace, matches id_cookie in array, , have variable begins id_cookie.
how can manage create preg_replace() replacing entire word ?
use word boundary:
$this->sql = preg_replace("/$parameter\b/", $variable, $this->sql); // here __^^
php regex preg-replace
Comments
Post a Comment