Return to Snippet

Revision: 25004
at March 17, 2010 14:37 by frankyfish


Initial Code
$str = "hola {all} todos {le} monde";
$ar = array("all" => "test");

function replaceTag($str, $replace) {
	preg_match_all("/\{(.[^\{\}]*)\}/", $str, $out);
	foreach($out[1] as $key => $value) {
		if(isset($replace[$value])) {
			$str = str_replace("{".$value."}", $replace[$value], $str);
		}
	}
	return $str;
}

echo replaceTag($str, $ar); // hola test todos {le} monde

Initial URL


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

Initial Title
PHP Replace Tag

Initial Tags


Initial Language
PHP