PDA

View Full Version : Regex to extract <?php //code here ?>


optimism_
06-01-2003, 01:51 PM
hi

I need a regex to extract multiline php code from an XML page. I know the xml-parser can parse the php itself, but it does so at the wrong time, so i need to extract the code and eval() it myslef at the right point. anyway, the regex i need needs to group as so:

<?(php) (//all my multiline code goes here;
//heres some more code
echo 'Summit';) ?>
would produce
$1 = "php"
$2 = "//all my multiline code goes here;
//heres some more code
echo 'Summit';"

my awful current method is to escape Every character except "?>" and check for that it is as follows:


$chars = quotemeta("$|=|>|<|?|+|\"|'|*|.|;|:|(|)|[|]|!|,|{|}|-");
$output_str = $str;
while(preg_match("/(?m)<\?([\w]+)\s+([\w|\s|\d|\r|\n|$chars|\\|\/|^(?>)]+)\s+\?>/", $output_str, $matches)){


There must be a better way than this because this is not 100% accurate, and id appreciate ANY help you can give me

optimism_
06-01-2003, 05:20 PM
never mind, i found it:

/<\?(\w+)\s+(.*?)\s+\?>/

hadnt realised *? made * ungreedy :o