By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,751 Members | 1,216 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,751 IT Pros & Developers. It's quick & easy.

method to parse CDATA as php

Expert Mod 5K+
P: 8,639

I've got a problem with a xsl output (see former xsl thread) resulting in php code (inserted via <xsl:processing-instruction>) printed out to the page and not parsed.

Right now I have a workaround for that, but I consider it rather, er, crude
Expand|Select|Wrap|Line Numbers
  1. <!-- previous html code from the site script -->
  2. <?php
  3. // $inhalt as instance of a custom class defined elsewhere
  4.     if ($text = $inhalt->DataInhalt()) {   // apply only if content comes from the xslt processing
  5.         $suche = '@<\?php([^\?]+)\?>@';    // get the PIs
  6.         preg_match_all($suche, $text, $treffer, PREG_OFFSET_CAPTURE);
  7.         $start = 0;
  8.         foreach ($treffer[0] as $key => $val) {
  9.             echo substr($text, $start, $val[1]-$start);  // split the text and print it
  10.             $start = $val[1] + strlen($val[0]);
  11.             eval(trim($treffer[1][$key][0]));     // process the php code
  12.         }
  13.         echo substr($text, $start);
  14.     }
  15. ?>
  16. <!--  further html code  -->
I know that the regex is not the best but '/<\?php(.+)\?>/' didn't work out.

I would have used something like preg_replace_callback() but callback functions can't use eval().

Unfortunately I'm not able to do the processing in the class itself, because the object that is to be processed in the xsl code produces an error (respectively a script exit) when called inside the outer class...

I've even tried to set the xsl result mime type to text/php but no response from the parser.

Any ideas to clean that code up are highly appreciated

Thanks in advance, Dormilich

(involved files are shown in the above mentioned thead)
Aug 28 '08 #1
Share this Question
Share on Google+
1 Reply

Expert Mod 5K+
P: 8,639
most problems solved

- found that in the regex the whitespace was making trouble, now using '@<\?php\s*(.+)\s*\?>@'

- calling the "inside" class now works (probably writing mistake)

- I realized that php is not likely to parse strings, so I have to save the result to a file if I want to do it without eval

Nevertheless, has someone an idea to overcome the callback obstacle?

thanks to myself *gg*
Sep 1 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.