PHP Replace Tag


/ Published in: PHP
Save to your folder(s)

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


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.