0

I lost a lot of time because of that issue, and I'm really resigned..

When I using preg_replace with create_function , all results going at the beggining of output, but they should be in the place of founded match, just like:

COMPONENT1 COMPONENT2 Maecenas malesuada elit lectus felis, malesuada ultricies. Curabitur et ligula. Ut molestie a, ultricies porta urna. Vestibulum commodo volutpat a, convallis ac, laoreet enim. Phasellus fermentum in, dolor. Pellentesque facilisis. Nulla imperdiet sit amet magna. Vestibulum dapibus, mauris nec malesuada fames ac turpis velit, rhoncus eu, luctus et interdum adipiscing wisi.

And should be:

Maecenas malesuada elit lectus felis, malesuada ultricies. Curabitur et ligula. Ut molestie a, ultricies porta urna. Vestibulum commodo volutpat a, convallis ac, laoreet enim. Phasellus fermentum COMPONENT1 in, dolor. Pellentesque facilisis. Nulla imperdiet sit amet magna COMPONENT2. Vestibulum dapibus, mauris nec malesuada fames ac turpis velit, rhoncus eu, luctus et interdum adipiscing wisi.

function getcomponent($source){
   $get = getXML(datadir.$source.'.xml')->content;
   $getcomp = create_function("",' ?>'.$get.'<?php ');
   $getcomp();
}

preg_match_all("/\[\#PHP (.*?) \#\]/",$content,$content3);

foreach ($content3[1] as $value) { //$content3[0]
    $content2 = preg_replace("/\[\#PHP ".$value." \#\]/",getcomponent($value),$content);
    $content = $content2;
}

I will be very grateful for every tip. Cheers!

Arek Dudys
  • 25
  • 4
  • I think by simplifying your code for the example you went a bit overboard. Few questions: Why would you use create function like that and then immediately call it? What is $content? In general what are you trying to do here? It seems you are getting XML from a file, what do you want to do with it? – Alfred_P Sep 07 '14 at 19:42
  • Hello! $content is a page content from XML file, Inside that content I have something like [# component1 #] and [# component2 #] and I want to replace it by content from component1.xml and component2.xml. But in that component xml's I have a plain PHP code, that I want to execute, thats why I use this create_function - its a override for eval() function – Arek Dudys Sep 07 '14 at 19:56

1 Answers1

0

Your getcomponent function should return the retrieved XML instead of printing it.

function getcomponent($source){
    $get = getXML(datadir.$source.'.xml')->content;
    $getcomp = create_function("",' ?>'.$get.'<?php ');
    ob_start();
    $getcomp();
    return ob_get_clean();

}

should do the trick.

Alfred_P
  • 792
  • 4
  • 8
  • Yes, You're right, but this way I got returned plain PHP code, not evaluated by PHP – Arek Dudys Sep 07 '14 at 20:17
  • updated the answer to use output buffering to get the output in a variable and return that. If you are using a framework make sure that it is not using output buffering already. – Alfred_P Sep 07 '14 at 20:27