Posted By

frankyfish on 03/17/10


Tagged


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

wirenaught


PHP Replace Tag


 / Published in: PHP
 

Find the tags {} and replace them by the value of the key in a array.

  1. $str = "hola {all} todos {le} monde";
  2. $ar = array("all" => "test");
  3.  
  4. function replaceTag($str, $replace) {
  5. preg_match_all("/\{(.[^\{\}]*)\}/", $str, $out);
  6. foreach($out[1] as $key => $value) {
  7. if(isset($replace[$value])) {
  8. $str = str_replace("{".$value."}", $replace[$value], $str);
  9. }
  10. }
  11. return $str;
  12. }
  13.  
  14. echo replaceTag($str, $ar); // hola test todos {le} monde

Report this snippet  

You need to login to post a comment.