Lumio Posted February 28, 2007 Share Posted February 28, 2007 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 Quote Link to comment Share on other sites More sharing options...
effigy Posted February 28, 2007 Share Posted February 28, 2007 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> Quote Link to comment Share on other sites More sharing options...
Lumio Posted March 1, 2007 Author Share Posted March 1, 2007 I don't want to use eval because I only want a few functions in my parser because I let users deside to use my php-parser or plain-html Quote Link to comment Share on other sites More sharing options...
effigy Posted March 1, 2007 Share Posted March 1, 2007 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> Quote Link to comment Share on other sites More sharing options...
Lumio Posted March 1, 2007 Author Share Posted March 1, 2007 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 ?> Quote Link to comment Share on other sites More sharing options...
effigy Posted March 1, 2007 Share Posted March 1, 2007 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> Quote Link to comment Share on other sites More sharing options...
Lumio Posted March 2, 2007 Author Share Posted March 2, 2007 Thanks a lot! I read your code... it's really easy... but it's a little tricky Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.