Help | Site Map
Connecting Tech Pros Worldwide
Reply
 
LinkBack Thread Tools
  #1  
Old August 28th, 2008, 10:45 AM
Dormilich's Avatar
Expert
 
Join Date: Aug 2008
Location: Leipzig, Germany
Age: 31
Posts: 641
Default method to parse CDATA as php

Hello,

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  -->
  17.  
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)
Reply
  #2  
Old September 1st, 2008, 09:24 AM
Dormilich's Avatar
Expert
 
Join Date: Aug 2008
Location: Leipzig, Germany
Age: 31
Posts: 641
Default method to parse CDATA as php

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*
Reply
Reply

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles