Click to See Complete Forum and Search --> : Parsing PHP from string with regex


YowZa
01-10-2007, 03:23 PM
Hello,

I would like to parse the PHP source from an HTML file, execute the PHP, and output the result as if the HTML document were indeed PHP. I have searched Google and all that I have been able to find is changing the script mapping for HTML to PHP but this is not an option for me. Most of the documents that will be parsed have HTML elements, but are not HTML documents-- just text files with mixed HTML and PHP.

I need to parse for all available PHP open tags including the short tags, eg, <?php, <?, and <?=. The content between these open tags and the closing tag ?> is what I assume I will need to execute using eval().

The test HTML file I am using is:

<html>
<head>
<title>PHP Parse Test</title>
</head>
<body>
<?php
echo 'php full tag test';
?>
<h1>This is a test.</h1>
<? echo 'php short tag test'; ?>
<p>This is another test<br />
<?='php short echo tag test'?></p>
</body>
</html>

The PHP code I am testing with:

<?php
$document = file_get_contents('test.html');
preg_match('/^(?:<\?php){1,1}(.*)(?:\?>){1,1}/ims',$document,$match);
print_r($match);
?>

The current result running this code is:

Array
(
[0] => <?php
echo 'php full tag test';
?>
<h1>This is a test.</h1>
<? echo 'php short tag test'; ?>
<p>This is another test<br />
<?='php short echo tag test'?>
[1] =>
echo 'php full tag test';
?>
<h1>This is a test.</h1>
<? echo 'php short tag test'; ?>
<p>This is another test<br />
<?='php short echo tag test'
)

I know part of my problem is my use of (.*), but I am uncertain how to express 'until a close tag is encountered' instead of 'until the very last close tag is encountered.'

Any help is greatly appreciated, and I am open to an alternative method that uses similar methods ( EDIT) or produces the intended results.

Thanks

bokeh
01-10-2007, 06:44 PM
Lazy quantifier: (.*?)

YowZa
01-16-2007, 12:15 AM
My apologies for the delay in a reply, but thank you very much. That worked like a charm. :)