note 84109 added to function.eval
| From: | krisdover at hotmail dot com | Date: | Sat, 28 Jun 2008 04:20:04 +0000 |
| Subject: | note 84109 added to function.eval | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-140891@lists.php.net to get a copy of this message | ||
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
----
Server IP: 202.89.58.1
Probable Submitter: 203.97.101.252
----
Manual Page -- http://www.php.net/manual/en/function.eval.php
Edit -- https://master.php.net/note/edit/84109
Del: integrated -- https://master.php.net/note/delete/84109/integrated
Del: useless -- https://master.php.net/note/delete/84109/useless
Del: bad code -- https://master.php.net/note/delete/84109/bad+code
Del: spam -- https://master.php.net/note/delete/84109/spam
Del: non-english -- https://master.php.net/note/delete/84109/non-english
Del: in docs -- https://master.php.net/note/delete/84109/in+docs
Del: other reasons-- https://master.php.net/note/delete/84109
Reject -- https://master.php.net/note/reject/84109
Search -- https://master.php.net/manage/user-notes.php