473,770 Members | 6,713 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Extract php code from a php file using RegEx

rizwan6feb
108 New Member
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 4104
Dormilich
8,658 Recognized Expert Moderator Expert
@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 New Member
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 Recognized Expert Moderator Expert
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
31497
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
3458
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 the most efficient way (using Reg Ex.) either than using the "indexOf" function of String. Thanks,
2
4492
by: Thief_ | last post by:
I've got this type of info on a web page: ---------------------------------------------------------------------------- -------------------------------------------- <tr height="25"> <td nowrap class="odd" align="center"><img src="/forums/images/icon_topic_new.gif" width=14 height=14 alt='New Topic' border=0></td> <td nowrap class="odd" align="center">&nbsp;</td>
1
6892
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 the text link as well. //sortlinks.php <?php $matches= array();
13
2721
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 ? As you can see the first truckname is "284165". The next truckname is "284193" Any help would be gratefully appreciated. Thanks,
7
2896
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 given word. In example from:
2
3101
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 9.8589e-05 1.4908e-04 8.3956e-04 3.8560e-03 4.8384e-02 11:40:01 499 499 1.0086e-03 2.4608e-04 9.8589e-05 1.4908e-04
1
1623
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 best approach in using regex for this? I can see match and replace the dates, extract section with regex, and then for each section extract again with the right regex the tables....
18
3973
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 of information that I don't need. Using the PHP function strip_tags I've ended with the text below and from the remaining HTML code, I'd like to extract the lines starting with "<TABLE BORDER="1" WIDTH="315">" up to its closing </TABLEtag. How do...
0
9591
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9425
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10001
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9867
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8880
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7415
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5312
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3969
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3573
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.