473,661 Members | 2,449 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using preg_replace to convert '<?php*?>' to nothing (removing all PHP code)

Hi,

I am trying to use preg_replace to take out all occurances of PHP code
after reading (fread()) the contents of a PHP file. The code I am using
is:

$html = preg_replace("< ?php*?>", "", $html);

Unfortuntely it is not working and giving the following error:
Warning: preg_replace(): Nothing to repeat for offset 0 in
C:/apache/...
Can someone please try this out and see what they get. How can I get
this to work?

Cheers

Burnsy

Sep 2 '05 #1
16 2885
On 2 Sep 2005 13:57:49 -0700, bi******@yahoo. co.uk wrote:
I am trying to use preg_replace to take out all occurances of PHP code
after reading (fread()) the contents of a PHP file. The code I am using
is:

$html = preg_replace("< ?php*?>", "", $html);


OK, what do you think that expression should match, and why?

--
Andy Hassall :: an**@andyh.co.u k :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Sep 2 '05 #2
Do you get the same error if you switch out <?php*?> with something
simple, as a test?

Sep 2 '05 #3
> $html = preg_replace("< ?php*?>", "", $html);

To my basic knowledge, that expression should match a string with
'<?php' at the start, and '?>' at the end with any collection of
characters inbetween (represented by the asterix - * - character).
Do you get the same error if you switch out <?php*?> with something

simple, as a test?

I did try and switch the '<?php*?>' part with just something like
'<h1>' and it seemed to work.

Sep 2 '05 #4
First, PCRE is interpreting the brackets as brackets for the
expression. Second, the question mark is a special character in RegEx
thus needs to be escaped. Third, to match any character zero or many
times you need ".*" and not just the asterisk.

The correct expression is "/<\?php.*?\?>/s". The s modifier is
necessary for matching across lines.

If this is intended as a security check of sort, then you need a lot
more code than a call to preg_replace().

Sep 2 '05 #5
>I am trying to use preg_replace to take out all occurances of PHP code
after reading (fread()) the contents of a PHP file. The code I am using
is:

$html = preg_replace("< ?php*?>", "", $html);


What characters when they are intended to be literal have to be
backslashed? This at least includes ? and *. Not sure about <>.

What does the * character mean in a regular expression? 0 or more
repetitions of what preceeds it. This is *NOT* the same as a
filename wildcard. p* matches a whole bunch of p's. As a filename
wildcard, it matches something that begins with p followed by
something else.

Offhand I'm having trouble finding documentation on the particular
flavor of regular expressions PHP uses.

Gordon L. Burditt
Sep 2 '05 #6
On 2 Sep 2005 14:46:41 -0700, bi******@yahoo. co.uk wrote:
$html = preg_replace("< ?php*?>", "", $html);
To my basic knowledge, that expression should match a string with
'<?php' at the start, and '?>' at the end with any collection of
characters inbetween (represented by the asterix - * - character).


It doesn't match that, it's an invalid pattern. Have you read:

http://uk2.php.net/manual/en/ref.pcre.php
http://uk2.php.net/manual/en/referen...ern.syntax.php
http://uk2.php.net/manual/en/referen....modifiers.php

Even if you'd surrounded it with delimiters, e.g. you'd used

/<?php*?>/

that would match (courtesy of the YAPE::Regex::Ex plain Perl module):

----------------------------------------------------------------------
<? '<' (optional (matching the most amount
possible))
----------------------------------------------------------------------
ph 'ph'
----------------------------------------------------------------------
p*? 'p' (0 or more times (matching the least
amount possible))
---------------------------------------------------------------------- '>'

----------------------------------------------------------------------
Do you get the same error if you switch out <?php*?> with something

simple, as a test?

I did try and switch the '<?php*?>' part with just something like
'<h1>' and it seemed to work.


But this doesn't match "<h1>" which it appears you might be expecting, it
matches "h1". Read http://uk2.php.net/manual/en/ref.pcre.php for why.

You may want to look at http://www.weitz.de/regex-coach/ which I've seen
mentioned a few times, and looks pretty useful for learning regexes from the
description and screenshot.

--
Andy Hassall :: an**@andyh.co.u k :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Sep 2 '05 #7
bi******@yahoo. co.uk wrote:
I am trying to use preg_replace to take out all occurances of PHP code
after reading (fread()) the contents of a PHP file. The code I am using
is:

$html = preg_replace("< ?php*?>", "", $html);


<?php

// ?>

?>

You're fired.

--
Jock
Sep 2 '05 #8
John Dunlop wrote:
<?php

// ?>

?>


Make that

<?php

/* ?> */

?>

You're still fired.

--
Jock
Sep 2 '05 #9
The block would be removed correctly if the greedy matching isn't
turned off.

Sep 3 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
4684
by: David | last post by:
Hi, I've had a search through google but couldn't really find the answer I was looking for.I'm new to PHP, so please take it <relatively> easy. I've created a script which runs some SNMP queries. I'd like help if possible in formatting one of the values it returns.
1
3703
by: Richard B. Christ | last post by:
I wrote the following code and it does NOT seem to work. $search = array('/<popup*>/ie'); $replace = array('make_popup(split_tag(\\0))'); preg_replace($search, $replace, $someText); If I try the following code, then the replacing seems to work (I just try to get a dummy back reference \\1) $search = array('/<p(op)up*>/ie');
3
4317
by: TXSherry | last post by:
Hi, I cannot seem to wrap my brain around preg_replace. Though I've read the help file backwords and forwards. :/ Hoping someone can give me a solution here. Problem: Given string 'str' which may contain new lines and will contain html code, IN this string any "words" that begin with an underscore I want to replace with a given word. A word here being a group of chars preceded by a space or null (start of line) and closed by a...
7
4994
by: Margaret MacDonald | last post by:
I've been going mad trying to figure out how to do this--it should be easy! Allow the user to enter '\_sometext\_', i.e., literal backslash, underscore, some text, literal backslash, underscore and, after submitting via POST to a preg_replace filter, get back '_sometext_' (i.e., the same thing with the literal backslashes stripped)
1
6156
by: WUV999U | last post by:
Hi I am fairly new to VBA and want to know how I can convert or I mean parse the XML file and convert to an access database. Please help. I greatly appreciate your time and help. I would be glad to have ur answers ASAP. Thanks a lot
13
7436
by: tperri | last post by:
I have an HTML table with several fields like this: <A href="Savings.aspx?category=Food"><asp:imagebutton id="imgFood" ImageUrl="images\buttons\btn-food-i.gif" runat="server"></asp:imagebutton></A> When the user sees these buttons, they will currently be on the savings.aspx?category=whatever page, and when they click another button to look at other categories, all the links I have around these buttons direct to their various categories.
23
14504
by: Bjorn | last post by:
Hi. Every time i post data in a form the contents are being checked for validity. When i click the back-button, all data is gone and i have to retype it. It's obvious that only a few or none of the visitors will retype it all so i'm asking: "how to preserve POST-data when clicking the back-button?" i've already tried to print post data as a value in a HTML tag but
14
3137
by: Michael | last post by:
Since the include function is called from within a PHP script, why does the included file have to identify itself as a PHP again by enclosing its code in <?php... <?> One would assume that the PHP interpreter works like any other, that is, it first expands all the include files, and then parses the resulting text. Can anyone help with an explanation? Thanks, M. McDonnell
1
6845
by: shalini jain | last post by:
Hi, I want to know how can we do pagination using XSL. There are number of tutorials available on pagination using PHP but nothing with XSL. i am really stuck with my code. Below is the code that i have written for pagination but it displays the link of all the pages at one go i.e. if i have 8 pages showing 10 results per page than it shows links for all 8 pages. Previous 1-10 11-20 21-30 31-40 41-50 51-60 61-70 71-80 Next i want to...
0
8432
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
8758
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8545
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
8633
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
7364
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
6185
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
5653
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4179
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...
0
4346
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.