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

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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 ?>

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.