note 18071 deleted from ref.xslt by sobak
| From: | sobak@php.net | Date: | Tue, 22 Nov 2016 03:31:28 +0000 |
| Subject: | note 18071 deleted from ref.xslt by sobak | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-207335@lists.php.net to get a copy of this message | ||
Note Submitter: Jaron dot Schaeffer at jayweb dot de
----
I extended the example of Daniel Unterberger even more. I had the problem to evaluate PHP code
within my xml data files (e.g. to output database results) and after that apply a xsl stylesheet to
the generated data.
When I tried Daniel's solution I found it quite useful but noticed that you can't
"print out" from your PHP Code. You're bound to annoying return()-Statements, wich
makes it impossible to loop outputs or concatenate strings and then return them. If you try to do
so, you will notice that a statement like
...
<php:eval>
for($i=0; $i<10; $i++)
echo "This is number ".$i;
</php:eval>
...
will result in PHP to do the output right in the moment when the eval()-Statement is run.
Things do, at least, work fine if you keep it simple:
...
<php:eval>
return("Hello World!");
</php:eval>
...
I tried to fix that and came to a solution. The following code is just a snippet from my XSLT-class.
The uppercase letters are constants defined in a header file. (e.g.
define('PHP_EVAL_START_TAG', '<php:eval>');
...
function xmlString() {
$fd = fopen($this->xmlFileLocation, 'r');
$xmlString = fread($fd, filesize($this->xmlFileLocation));
fclose($fd);
if($this->contentType == DYNAMIC_CONTENT) {
while(!(strpos($xmlString, PHP_EVAL_START_TAG) === false)) {
$startPos = strpos($xmlString, PHP_EVAL_START_TAG);
$endPos = strpos($xmlString, PHP_EVAL_END_TAG);
ob_start();
eval(substr($xmlString, $startPos + strlen(PHP_EVAL_START_TAG), $endPos - $startPos -
strlen(PHP_EVAL_START_TAG)));
$evaluatedString = ob_get_contents();
ob_end_clean();
$xmlString =
substr($xmlString, 0, $startPos).$evaluatedString.substr($xmlString, $endPos + 11);
}
}
return($xmlString);
}
...
Jaron.Schaeffer@jayweb.de