Page is a not externally linkable
- Code, Content, and Presentation
-- PHP Server Side Scripting
---- The One Line Template Engine!


vincevincevince - 11:07 am on Sep 12, 2007 (gmt 0)


> why not use str_replace

Exactly as dmorison said - to make it generic. It's not normally expected that you have to list the tags which you're going to use. Although, if all fields are defined within an array such as $values['{title}'], $values['{header}'], etc. then you could use array_keys($values) and $values as the arguments to your str_replace making it more generic.

The str_replace would probably be faster for some cases, although it would depend upon the number of tags to be replaced. On the positive side for the str_replace, it would be more secure if you didn't have control over your .tpl file.

One difference in end result is that with the preg_replace method, a tag enclosed in {} but not matched by a variable will be removed ("Hello {name}!" => "Hello!"), whereas the str_replace method would leave that unreplaced tag in place ("Hello {name}!" => "Hello {name}!")


Thread source:: http://www.webmasterworld.com/php/3444822.htm
Brought to you by WebmasterWorld: http://www.webmasterworld.com