473,320 Members | 2,112 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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 2859
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.uk :: 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::Explain 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.uk :: 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
bi******@yahoo.co.uk wrote:
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:

<snip>

Are you aware of <http://in2.php.net/strip_tags> and
<http://in2.php.net/tokenizer> ?

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

Sep 3 '05 #11
Chung Leong wrote:
The block would be removed correctly if the greedy matching isn't
turned off.


What greedy matching?

Anyway, on or off, it doesn't matter.

<?php

?>

foo

<?php

?>

He never got the job.

--
Jock
Sep 3 '05 #12
In article <d6***************@newsfe7-win.ntli.net>,
John Dunlop <us*********@john.dunlop.name> wrote:
Chung Leong wrote:
The block would be removed correctly if the greedy matching isn't
turned off.


What greedy matching?

Anyway, on or off, it doesn't matter.

<?php

?>

foo

<?php

?>

He never got the job.


#!/usr/bin/php
<?
$strings = array(
"Hello <?php print 'Miss'; ?> World",
"Hello <?php print 'World'; ?>",
"<?php echo 'foo'; ?>Hello World <?php echo 'bar'; ?>",
);
foreach (preg_replace("/<\?php.*?\?>/si", "", $strings) as $line)
print "$line\n";
?>

OUTPUT:

Hello World
Hello
Hello World

--
Sandman[.net]
Sep 3 '05 #13
Sandman wrote:
#!/usr/bin/php
<?
$strings = array(
"Hello <?php print 'Miss'; ?> World",
"Hello <?php print 'World'; ?>",
"<?php echo 'foo'; ?>Hello World <?php echo 'bar'; ?>",
);
foreach (preg_replace("/<\?php.*?\?>/si", "", $strings) as $line)
print "$line\n";
?>

OUTPUT:

Hello World
Hello
Hello World


I can't reread this subthread for you.

--
Jock
Sep 3 '05 #14
In article <1X***************@newsfe7-win.ntli.net>,
John Dunlop <us*********@john.dunlop.name> wrote:
Sandman wrote:
#!/usr/bin/php
<?
$strings = array(
"Hello <?php print 'Miss'; ?> World",
"Hello <?php print 'World'; ?>",
"<?php echo 'foo'; ?>Hello World <?php echo 'bar'; ?>",
);
foreach (preg_replace("/<\?php.*?\?>/si", "", $strings) as $line)
print "$line\n";
?>

OUTPUT:

Hello World
Hello
Hello World


I can't reread this subthread for you.


I wouldn't have expected such ability from you, no.

--
Sandman[.net]
Sep 3 '05 #15
JDS
On Fri, 02 Sep 2005 18:47:46 -0700, Chung Leong wrote:
The block would be removed correctly if the greedy matching isn't
turned off.


No it wouldn't

--
JDS | je*****@example.invalid
| http://www.newtnotes.com
DJMBS | http://newtnotes.com/doctor-jeff-master-brainsurgeon/

Sep 6 '05 #16
JDS
On Fri, 02 Sep 2005 21:51:06 +0000, Gordon Burditt wrote:
Offhand I'm having trouble finding documentation on the particular
flavor of regular expressions PHP uses.


PHP uses a couple of different flavors

the preg_*() functions are Perl-compatible, meaning they use regex syntax
the way Perl uses regex syntax.

ereg_* is POSIX compliant, I believe

--
JDS | je*****@example.invalid
| http://www.newtnotes.com
DJMBS | http://newtnotes.com/doctor-jeff-master-brainsurgeon/

Sep 6 '05 #17

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

Similar topics

4
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...
1
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...
3
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'...
7
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...
1
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...
13
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"...
23
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...
14
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...
1
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...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.