Extremly compilcated regex/coding problem.
Posted: Mon Jul 16, 2007 9:23 pm
First an example:
Code:
Here's the problem... lets say this is the code for a web page and I'm trying to determine which parts are php and which parts are html. If I just split the string at every occurrence of <? or <?php or ?>, obviously there are going to be problems...
I highly doubt this could be done in a single regular expression, but multiple ones maybe. First step seems to be to detect where strings are in $code and ignore those areas, but then again, what if html contains something like...
As you can see, if all string areas are ignored, some valid php code may also be ignored.
I've gotten as far as developing the following code. However, even with my best attempt, I'm unable to produce the desired results.
Output is:
Should be:
The following string should contain every possible problem that could be encountered
If you can build a function that will turn that into the array of what it should be... I would consider you a God of PHP
Code:
Code: Select all
$code = 'Some html here <? echo\'PHP code always starts with <? or <?php, and ends with ?>\'; ?> more html';I highly doubt this could be done in a single regular expression, but multiple ones maybe. First step seems to be to detect where strings are in $code and ignore those areas, but then again, what if html contains something like...
Code: Select all
<form method="POST" action="<?php echo$_SERVER['PHP_SELF']?>">I've gotten as far as developing the following code. However, even with my best attempt, I'm unable to produce the desired results.
Code: Select all
function findPHP($input){
$pieces = preg_split('/(<\?.+?\?>)/s', $input, -1, PREG_SPLIT_DELIM_CAPTURE);
$revised_pieces = array();
for($i=0;$i<count($pieces);$i++){
$piece = $pieces[$i];
$quotes = 0;
preg_replace('/(?<!\\\)["\']/', '', $piece, -1, $quotes);
$revised_pieces[$i] = $piece;
if (strpos($piece, '<?') === FALSE)
continue;
if ($quotes % 2) {
list($before, $after) = preg_split('/(?<=\?>)/', $pieces[$i+1]);
$revised_pieces[$i] .= $before;
$revised_pieces[$i+1] = $after;
$i++;
}
}
foreach($revised_pieces as $piece)
if(strlen($piece)!=0)
$output[] = $piece;
return $output;
}
print_r(findPHP('html"<?php?>"html<?php"?><?"php?>html ?> end'));
?>Code: Select all
Array
(
[0] => html"
[1] => <?php?>
[2] => "html
[3] => <?php"?>
[4] => <?"php?>html ?>
[5] => end
)Code: Select all
Array
(
[0] => html"
[1] => <?php?>
[2] => "html
[3] => <?php"?><?"php?>
[4] => html ?> end
)Code: Select all
html"<?php?>"html<?php"?><?"php?>html ?> end