472,958 Members | 1,958 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

Extract php code from a php file using RegEx

rizwan6feb
108 100+
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
Jun 8 '09 #1
3 4057
Dormilich
8,658 Expert Mod 8TB
@rizwan6feb
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)
Jun 11 '09 #2
rizwan6feb
108 100+
Found a solution at http://regexadvice.com/forums/thread/53756.aspx

The regex below is what i needed
<\?(\x22[^\x22]*?\x22|\x27[^\x27]*?\x27|/\*.*?\*/|.)*?\?>
Jun 12 '09 #3
Dormilich
8,658 Expert Mod 8TB
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. <\?("[^"]*?"|'[^\']*?'|/\*.*?\*/|.)*?\?>
Jun 12 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: SamIAm | last post by:
How do I extract the last 3 characters of a string i.e. string s = "000001" I want "001" Thanks, S
1
by: Ori | last post by:
Hi, I have a HTML text which I need to parse in order to extract data from it. My html contain a table contains few rows and two columns. I want to extract the data from the 2nd column in...
2
by: Thief_ | last post by:
I've got this type of info on a web page: ---------------------------------------------------------------------------- -------------------------------------------- <tr height="25"> <td nowrap...
1
by: kidkurious | last post by:
I have a script that will read web file, extract the hyperlinks and sort them in alphabetical order. It works fine, but not the way I want. I want to change the script so that it will extract...
13
by: Tony Girgenti | last post by:
Hello. Using VS.NET 2003 VB. If i have a string similar to the attached, how would i extract the "Truckname=" data from it in a loop and stay in the loop until the end of the string is reached...
7
by: teo | last post by:
hallo, I need to extract a word and few text that precedes and follows it (about 30 + 30 chars) from a long textual document. Like the description that Google returns when it has found a...
2
by: Fabian Braennstroem | last post by:
Hi, I would like to delete a region on a log file which has this kind of structure: #------flutest------------------------------------------------------------ 498 1.0086e-03 2.4608e-04...
1
by: GS | last post by:
I need to extract sections out of a long string of about 5 to 10 KB, change any date format of dd Mmm yyyy to yyyy-mm-dd, then further from each section extract columns of tables. what is the...
18
by: Ecka | last post by:
Hi everyone, I'm trying to write a PHP script that connects to a bank's currency convertor page using cURL and that part works fine. The issue is that I end up with a page that includes a lot...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.