Jump to content

[SOLVED] PHP-Parser with PHP


Lumio

Recommended Posts

Hello!

I need a way to do a simple PHP-Parser for my users.

The problem I see is, how to find PHP-Blocks.

I know, it isn't that hard but I want to here your opinions.

 

I think there are 3 ways to solve that and find PHP-Blocks:

  • I face every sign and find out if it's a PHP-Block... so if there is a < I mark that point and look to the next sign... if its a ? it's a PHP-Block. Now I can repeat that facing and find out if ?> is in a quote or not.
  • I look up <? and ?> and watch comments and quotes before ?>. The first one is much easier.
  • I make that with RegEx. The only problem: how to find out, if ?> is in a quote or comment?

 

Thanks for your upcomming ideas ;)

Link to comment
https://forums.phpfreaks.com/topic/40608-solved-php-parser-with-php/
Share on other sites

Something like this?

 

<pre>
<?php

$mixture = <<<MIX
		<html>
			<?php \$title = 'ABC'; ?>
			<head>
				<title><?php echo \$title; ?></title>
			</head>
			<body>
				Today is <?php \$date = getdate(); echo \$date['weekday']; ?>.
			</body>
		</html>
MIX;

echo eval('?>' . $mixture);

?>
</pre>

Split the data between php and non-php, then loop through each:

 

<pre>
<?php

$mixture = <<<MIX
		<html>
			<?php \$title = 'ABC'; ?>
			<head>
				<title><?php echo \$title; ?></title>
			</head>
			<body>
				Today is <?php \$date = getdate(); echo \$date['weekday']; ?>.
			</body>
		</html>
MIX;

$pieces = preg_split('/(<\?.+?\?>)/', $mixture, -1, PREG_SPLIT_DELIM_CAPTURE);
print_r($pieces);

?>
</pre>

Okay! That works... but not as well... what if $foo = '?>'... so:

<?
   $code = '<html>
                   <head>
                      <title>foo=bar</title>
                   </head>
                   <body>
                      <? echo 'blubb ?>'; ?>
                   </body>
                </html>';

Then it gets <? echo 'blubb ?>

How about this?

 

<pre>
<?php

$mixture = <<<MIX
		<html>
			<?php \$title = 'ABC?>'; ?>
			<head>
				<title><?php echo \$title; ?></title>
			</head>
			<body>
				Today is <?php \$date = getdate(); echo \$date['weekday']; ?>.
			</body>
		</html>
MIX;

$pieces = preg_split('/(<\?.+?\?>)/', $mixture, -1, PREG_SPLIT_DELIM_CAPTURE);
$revised_pieces = array();
$num_pieces = count($pieces);
### Loop through and fix the matches.
for ($i = 0; $i <= $num_pieces; $i++) {
	$piece = $pieces[$i];
	### Count the number of non-backslashed quotes.
	$quotes = 0;
	preg_replace('/(?<!\\\)["\']/', '', $piece, -1, $quotes);
	### Always add the current piece being processed.
	$revised_pieces[$i] = $piece;
	### If the quotes are uneven...
	if ($quotes % 2) {
		### Split apart the next piece.
		list($before, $after) = preg_split('/(?<=\?>)/', $pieces[$i+1]);
		### Add the missing end to this piece.
		$revised_pieces[$i] .= $before;
		### Add the rest to the next piece.
		$revised_pieces[$i+1] = $after;
		### Skip processing of the next piece.
		$i++;
	}
}
print_r($revised_pieces);	
?>
</pre>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.