Hiya, my first post here!
Building a very simple CMS and want a way for the layout pages to insert whatever bits of the content they require.
For example [_title] inserts the title, [_lastmod] the date the page was last modified, etc. All these are fetched from mysql as an array at the moment and are sorted using str_replace, but hardcoding every field of data is a bit tedius.
I would like to use the following to change any [_varname] to whatever is stored in $varname.
preg_replace("/\[_(\w*)\]/", "\$$1", $layout[0]);
\$$1 simply prints '$varname' obviously, not quite what im looking for!
many thanks.
Replace string with $var of the same name
Moderator: General Moderators
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
PHP will not evaluate the replacement string as PHP code. Although there is the e modifier for patterns to do an evaluation, I cannot recommend there use as it is a security hole. Instead, I'd suggest using preg_replace_callback() to handle it.