仅从PHP文件中获取源代码

Is there any clean grep one-liner to grep only the PHP source (the code between <? and ?>) from a PHP file? Thus far I've tried:

grep -oiE "<\?php[[:print:]]*\?>" name_of_php_file

But it seems to return muddled entries like:

<?php print $row[0];?>"><img src="gallerybig/<?php echo $row[1];?>

Expecting a one-liner which would return only PHP source, without any muddled entries. sed and awk are OK.


EDIT: The expected output is PHP source code, even if it spans multiple lines. The sample output is bad, as it contains HTML in between: "><img src="gallerybig/


EDIT 2: The answer given by @SigmaPiEpsilon works, but grep doesn't print code that spans multiple lines. For example, this snippet of code doesn't show up in the output:

<?php 
$str="select c_id,c_description,cate_name,date from book_table ;";
$res=mysql_query($str); ?>

You have to use a non-greedy search by adding a ? after the [[:print:]]*. This is not available in normal grep but available with Perl syntax by specifying -P flag instead of -E. Check man grep to make sure your grep supports this.

$ Var='<?php print $row[0];?>"><img src="gallerybig/<?php echo $row[1];?>'
$ echo $Var | grep -oiP "<\?php[[:print:]]*?\?>"
<?php print $row[0];?>
<?php echo $row[1];?>

To match this over multiple lines you have to specify a newline character and use the -z flag. The regex gets a little tricky, but instead of specifying non-greedy match with ? you can use the complement match as [^?] which matches everything except ?. See below

$ cat test_php 
<?php 
$str="select c_id,c_description,cate_name,date from book_table ;";
$res=mysql_query($str); ?>
<?php print $row[0];?>"><img src="gallerybig/<?php echo $row[1];?>
$ grep -oizP "<\?php[^?]*
*\?>" test_php
<?php 
$str="select c_id,c_description,cate_name,date from book_table ;";
$res=mysql_query($str); ?>
<?php print $row[0];?>
<?php echo $row[1];?>

You can do what you've described so far with GNU awk:

$ gawk -v RS='?>' 'RT{sub(/.*<\?/,"<?"); print $0 RT}' file
<?php
$str="select c_id,c_description,cate_name,date from book_table ;";
$res=mysql_query($str); ?>

but it's obviously not robust since that wouldn't account for ?> inside a comment or inside string or inside HTML or... so whether or not it'll do what you want depends on the non-trivial cases that you haven't shown us yet IF they exist in your real input.