$text = 'Why hello there {name}, how are you today?'; $name = 'Brad';
$text = str_replace('{name}', $name, $text); echo $text; // Why hello there Brad, how are you today?
Or were you looking for something more generic? If so, you could do a preg_match_all() to find all occurrences of {something} (where 'something' looks like whatever you want, e.g. a valid PHP variable name). You could then walk through the array of matches and use isset() to determine if a PHP variable exists and, if so, str_replace() the placeholder with its value.
There are various templating engines out there that use a similar syntax. Probably the most popular is Smarty, which to simply output a PHP variable uses something like:
Code:
<p>Hello, {$username}.</p>
This is not built-in functionality for PHP -- you would need to download and include Smarty to have it parse such template files.
Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be." ~ from Nation, by Terry Pratchett
"But the main reason that any programmer learning any new language thinks the new language is SO much better than the old one is because he’s a better programmer now!" ~ http://www.oreillynet.com/ruby/blog/...ck_to_p_1.html
Bookmarks