php - use preg_replace to replace whole words using associative array -
i have replacement array named $initialdata : 
array ($initialdata)   'd' => string '1.40' (length=4)   'a' => string '1.67' (length=4)   'vi' => string '0' (length=1)   't' => string '?' (length=1) then have string :
$str =  "-(vi + sqrt(2*a*d + vi^2))/a)"; when :
str_replace(array_keys($initialdata),array_values($initialdata),$str); i :
-(0 + sqr?(2*1.67*1.40 + 0^2))/1.67) what happened "t" of "sqrt" replaced value of "t" on $initialdata array. know happens because i'm using str_replace, , need match whole words using preg_replace, never saw implementation of preg_replace using associative arrays match whole word. how can achieved if possible?
in regex, \b word boundaries. should work:
$data = array(   '/d/' => '1.40',   '/a/' => '1.67',   '/vi/' => '0',   '/\bt\b/' => '?' );  $result = preg_replace(array_keys($data), array_values($data), $str); 
Comments
Post a Comment