note 36926 added to function.preg-replace-callback

From: Date: Mon, 27 Oct 2003 16:05:25 +0000
Subject: note 36926 added to function.preg-replace-callback
Groups: php.notes 
Request: Send a blank email to php-notes+get-59383@lists.php.net to get a copy of this message
Here's an example of how to evaluate PHP code inside a HTML block. In my example I have two timestamps which I insert at several places in a HTML-template. The formatting for the dates are put inside the template. When the insert is done the code is evaluated and the dates are formatted. All code inside "<php>" to "</php>"-tags will be evaluated. ----------------------------------------------------------- function evalcode($matches) { eval("\\$matches[2] = $matches[2];"); return $matches[2]; } $template = "<html><body>This really simple HTML-page that was created at <php>date('Y-m-d',{datecreated})</php> and modified at <php>date('H:i:s',{datemodified})</php> on the <php>date('dS',{datemodified})</php> of <php>date('F Y',{datemodified})</php>.</body></html>"; $datecreated = 1065162746; $datemodified = time(); $output = $template; $output = str_replace('{datecreated}', $datecreated, $output); $output = str_replace('{datemodified}', $datemodified, $output); $output = preg_replace_callback("/(<php>)(.*)(<\\/php>)/U", "evalcode", $output); echo $output; ---------------------------------------------------------- The output will be something like "This really simple HTML-page that was created at 2003-10-03 and modified at 16:58:31 on the 27th of October 2003." ---- Manual Page -- http://www.php.net/manual/en/function.preg-replace-callback.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+36926 Delete -- http://master.php.net/manage/user-notes.php?action=delete+36926&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+36926&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#59383) next »