php


Best practice to substitute values on a page (tokens), without using (pure) PHP?


I am in currently upgrading a simple templating system for users who create their own pages, and would like them to be able to insert a set of their own pre-set "variables".
Here is the workflow:
Client textarea --> saved to database --> pulled from database --> page generated
Now here's an example of what their textarea might look like:
Hello my name is {{firstname}} {{lastname}}. My phone number is {{phone}}.
Where of course the values of firstname lastname and phone all currently sit in a session variable. And either when the page is saved to the database or page generated we need to replace {{firstname}} {{lastname}} and {{phone}} with items stored in the session.
What is being done now is:
$pat = "/\{\{$match\}\}/"; //where $match is a preg_match for the rendered html to see if there's a "token"
if ($match === 'FirstName') {
if (empty($firstname)) { $err[] = '{FirstName}'; } $str = preg_replace($pat, $firstname, $str);
}
if ($match === 'LastName') {
if (empty($lastname)) { $err[] = '{LastName}'; } $str = preg_replace($pat, $lastname, $str);
}
The way that it's being done at the moment, is a convoluted bunch of code including preg_match and preg_replace -- which I know isn't the "correct" or most efficient way to do this. Is there any pre-set function in PHP to set your own token patterns, and observe them for a match? Or perhaps a module that could be installed? A class or observer?
I want to reiterate that I am not looking for a purely programmatic solution -- As this has already been done. I am wondering if there's a pre-fabbed PHP solution built in that solves this specific quandary? Or am I better off creating my own class and building this token machine from scratch?
I have found this page, but it only pertains to Drupal, and I am not using any out-of the-box CMS on this particular occasion.

Related Links

PHP OOP Database Class - Is it really needed? [closed]
Setup POST variables and the Redirect Page
jQuery, items not being deleted form cart correctly
php: trying to call data from an object
Is converting mysql to mysqli extremely necessary?
Trouble in logic with mysql
jQuery not returning JSON data
Facebook opengraph - get latest 5 updated posts from Facebook group?
PHP sql security
page hangs when i repeat image 10000 times.. any alternate jquery or javascript solution is needed [duplicate]
Retrieve the time of pressing signup button
Extract cell value from table based on another cells value
Insert record into MYSQL database with PHP
PHP determine longest value within a loop using strlen
Drawing a pie chart from mysql db
wordpress widget drag and drop not working after update to 3.5.1

Categories

HOME
generics
windows-8.1
focus
barcode-scanner
bots
activemq
odata
swift2
drag-and-drop
facebook-android-sdk
tup
cocotb
sharepoint-online
hspi
stack-overflow
parsley.js
stimulsoft
twisted
temperature
pywinauto
significance
opam
libgit2
node-webkit
quartz.net
hevc
openbravo
math.js
mdns
google-play-developer-api
fotorama
buildforge
android-alarms
strapi
microkernel
uipickerview
coverage.py
dbcc
morphline
oracle-bmcs
adblock
dredd
icecast
comm
sequence-diagram
winmerge
glimpse
emf-compare
ltrace
nrf51
pbx
metabase
conan
ajaxmin
sharepoint-apps
teamviewer
vimperator
ioctl
teensy
juniper-network-connect
google-maps-api-2
apache-commons-digester
blank-line
domino-designer-eclipse
angular-amd
deque
subfolder
sql-processor
zend-mail
producer
berkeley-db-xml
relationships
oracle-adf-mobile
django-sites
application-blocks
oscilloscope
ofstream
ckeditor.net
clearinterval
first-class
platform-independent
liveconnect
firefly-mv
nsconnection
ninject-interception
outlook-2007-addin
asdoc
oracle-pro-c
google-translator-toolkit
fxruby
jvm-bytecode

Resources

Encrypt Message