Connecting Tech Pros Worldwide Help | Site Map

Extract php code from a php file using RegEx

rizwan6feb's Avatar
Member
 
Join Date: Jul 2007
Posts: 106
#1: Jun 8 '09
I am trying to extract php code from a php file (php file also contains html, css and javascript code). I am using the following regex for this
Expand|Select|Wrap|Line Numbers
  1. <\?[\w\W]*?\?>
  2.  
but this doesn't cater quotation marks (single and double quotes) and comments, i mean how can i skip php tags inside a string (and comments). Please have a look at the following code
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.     include("db.php");
  3.     $name=$_REQUEST['name'];
  4.     /* the regular expression to extract code inside php tags (i.e  <? and ?>) is  */
  5.     $str='|<\?[\w\W]*?\?>|';
  6.     # The regex can also be written using double quotes
  7.     $str="|<\?[\w\W]*?\?>|";
  8. ?>
  9. <html xmlns="http://www.w3.org/1999/xhtml">
  10. <head>
  11.  
  12. <title>Sample PHP File</title>
  13. </head>
  14.  
  15. <body>
  16. <?="<h1>Some output from PHP?>
  17. </body>
  18. </html>
  19.  
I need a regular expression that extracts the two blocks of PHP code from sample code
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,631
#2: Jun 11 '09

re: Extract php code from a php file using RegEx


Quote:

Originally Posted by rizwan6feb View Post

Expand|Select|Wrap|Line Numbers
  1. <\?[\w\W]*?\?>
but this doesn't cater quotation marks (single and double quotes) and comments,

well, if I try, the RegEx gets it all. your problem is that it is extremely difficult to determine, whether a "?>" is a comment, a string or a processing instruction (i.e. you need to kind of parse the string).

maybe it's easier to do the reverse and not extract what's between ?> and <? (though this may also fail in special circumstances)
rizwan6feb's Avatar
Member
 
Join Date: Jul 2007
Posts: 106
#3: Jun 12 '09

re: Extract php code from a php file using RegEx


Found a solution at http://regexadvice.com/forums/thread/53756.aspx

The regex below is what i needed
<\?(\x22[^\x22]*?\x22|\x27[^\x27]*?\x27|/\*.*?\*/|.)*?\?>
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,631
#4: Jun 12 '09

re: Extract php code from a php file using RegEx


are you sure? the RegEx fails for me (that is, it doesn't get the whole first block, only chunks).

translated into characters… all that's a string, a comment or any char
Expand|Select|Wrap|Line Numbers
  1. <\?("[^"]*?"|'[^\']*?'|/\*.*?\*/|.)*?\?>
Reply


Similar PHP bytes