preg_replace using pattern as index of replacement

2019-04-25 22:43发布

问题:

I would like to know if there is a simple way to use the matched pattern in a preg_replace as an index for the replacement value array.

e.g.

preg_replace("/\{[a-z_]*\}/i", "{$data_array[\1]}", $string);

Search for {xxx} and replace it with the value in $data_array['xxx'], where xxx is a pattern.

But this expression does not work as its invalid php.

I have written the following function, but I'd like to know if it is possible to do it simply. I could use a callback, but how would I pass the $data_array to it too?

function mailmerge($string, $data_array, $tags='{}')
{
    $tag_start=$tags[0];
    $tag_end  =$tags[1];
    if( (!stristr($string, $tag_start)) && (!stristr($string, $tag_end)) ) return $string;

    while(list($key,$value)=each($data_array))
    {
        $patterns[$key]="/".preg_quote($tag_start.$key.$tag_end)."/";
    }
    ksort($patterns);
    ksort($data_array);

    return preg_replace($patterns, $data_array, $string);
}

回答1:

From my head:

preg_replace_callback("/\{([a-z_]*)\}/i", function($m) use($data_array){
  return $data_array[$m[1]];
}, $string);

Note: The above function requires PHP 5.3+.



回答2:

Associative Array replacement - keep matched fragments if not found:

$words=array("_saudation_"=>"Hello", "_animal_"=>"cat", "_animal_sound_"=>"MEooow");

$source=" _saudation_! My Animal is a _animal_ and it says _animal_sound_ ,  _no_match_";

echo (preg_replace_callback("/\b_(\w*)_\b/", function($match) use ($words) { if(isset($words[$match[0]])){
return ($words[$match[0]]);}else{
return($match[0]);} 
},  $source));

    //returns:  Hello! My Animal is a cat and it says MEooow ,  _no_match_

*Notice, thats although "_no_match_" lacks translation, it will match during regex, but preserve its key.



回答3:

you can use preg_replace_callback and write a function where you can use that array index, or else you can use the e modifier to evaluate the replacement string (though note that the e modifier is deprecated, so the callback function is better solution).