note 84109 deleted from function.eval by danbrown
| From: | danbrown@php.net | Date: | Sun, 17 May 2009 19:52:13 +0000 |
| Subject: | note 84109 deleted from function.eval by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-155282@lists.php.net to get a copy of this message | ||
Note Submitter: krisdover at hotmail dot com
----
As critmas at hotmail dot com describes in an early posts, you can treat php tags as processing
instructions if you need to evaluate some php in an xml/xhtml document, e.g. for templating. Note
this is limited to simple php statement evaluation and would not work with content which uses
braces '{', you'll need to use include() for that. An example of this follows:
<?php
// setup a test document with some xhtml
$doc = new DOMDocument();
$doc->loadXML('<div><h1><?php echo $title; ?></h1><p><?php
echo $msg; ?></p></div>');
// execute an xpath query to get all php scriplets
$xpath = new DOMXPath($doc);
$scripts = $xpath->query("descendant::processing-instruction(\"php\")");
// define our placeholder variables
$title = "foobar";
$msg = "a foo walks into a bar ...";
// fill in the template
foreach($scripts as $script){
ob_start();
eval($script->data);
$result = ob_get_contents();;
ob_end_clean();
$script->parentNode->replaceChild($doc->createTextNode($result), $script);
}
echo $doc->saveXML();
?>
The result is:
<?xml version="1.0"?>
<div><h1>foobar</h1><p>a foo walks into a bar ...</p></div>
Regards,
Kris