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