note 112747 added to language.variables.variable

From: Date: Fri, 19 Jul 2013 09:33:02 +0000
Subject: note 112747 added to language.variables.variable
Groups: php.notes 
Request: Send a blank email to php-notes+get-195672@lists.php.net to get a copy of this message
Parse an imported string e.g. from a database for php variable names and replace them with their values. example: In the String "Dear $firstname $lastname, welcome to our Homepage" the variables shall be replaced with the respective values. <?php $welcome = getImportedString(); $firstname = "David"; $lastname = "Forger"; echo $welcome; // before replacement output will be: //"Dear $firstname $lastname, welcome to our Homepage" function replaceVars($match) { return $GLOBALS[$match[1]]; } $welcome = preg_replace_callback('/\$(\w+)/i', "replaceVars", $welcome); echo $welcome; //now output will be: //"Dear David Forger, welcome to our Homepage" ?> ---- Server IP: 69.147.83.199 Probable Submitter: 213.218.16.130 ---- Manual Page -- http://php.net/manual/en/language.variables.variable.php Edit -- https://master.php.net/note/edit/112747 Del: integrated -- https://master.php.net/note/delete/112747/integrated Del: useless -- https://master.php.net/note/delete/112747/useless Del: bad code -- https://master.php.net/note/delete/112747/bad+code Del: spam -- https://master.php.net/note/delete/112747/spam Del: non-english -- https://master.php.net/note/delete/112747/non-english Del: in docs -- https://master.php.net/note/delete/112747/in+docs Del: other reasons-- https://master.php.net/note/delete/112747 Reject -- https://master.php.net/note/reject/112747 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#195672) next »