By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,725 Members | 1,040 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,725 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
16 Replies


P: n/a
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

P: n/a
Do you get the same error if you switch out <?php*?> with something
simple, as a test?

Sep 2 '05 #3

P: n/a
> $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

P: n/a
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

P: n/a
>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

P: n/a
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

P: n/a
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

P: n/a
John Dunlop wrote:
<?php

// ?>

?>


Make that

<?php

/* ?> */

?>

You're still fired.

--
Jock
Sep 2 '05 #9

P: n/a
The block would be removed correctly if the greedy matching isn't
turned off.

Sep 3 '05 #10

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.