简体   繁体   中英

Help with Regular expression (PHP, preg_replace)

I need to do a preg_replace on all of the PHP tags in a string, as well as any characters sitting between the PHP tags.

Eg, if the file contents was:

Hey there!
<?php some_stuff() ?>
Woohoo!

All that should be left is:

Hey there!
Woohoo!

Here's my code:

$file_contents = file_get_contents('somefilename.php');
$regex = '#([<?php](.*)[\?>])#e';
$file_contents = preg_replace($regex, '<<GENERATED CONTENT>>', $file_contents);

FAIL.

My regular expression skills are poor, can someone please fix my regex. Thank you.

Try this regex:

#<\?.*?\?>#

Should work on short tags (without 'php') too.

I think the main issue with your attempt was that you need to escape the question marks with backslashes, and that you were using square brackets where you shouldn't have been. Square brackets means "pick any one of these characters".

$regex="/<?php (.*?)?\>/"

您也可以尝试一下,这将为您工作

You can try:

$regex = '#<\?php.*?\?>#i';

The regex used: <\\?php.*?\\?>

  • < : a literal <
  • \\? : ? is a metachar to match a literal ? you need to escape it.
  • .*? : non-greedy to match anything.

Use the right tool for the job. The PHP tokenizer contains all the functionality you need to strip PHP code away from the surrounding content:

source.php

<p>Some  HTML</p>
<?php echo("hello world"); ?>
<p>More HTML</p>
<?php
/*
 Strip this out please
 */
?>
<p>Ok Then</p>

tokenize.php

<?php
$source = file_get_contents('source.php');
$tokens= token_get_all($source);
foreach ($tokens as $token) {
 if ($token[2] == 3 || $token[2] == 1 || $token[2] == 9) {
    echo($token[1]);
 }
}

Output:

<p>Some  HTML</p>
<p>More HTML</p>
<p>Ok Then</p>

This is a simple example. The docs list all the parser tokens you can check for.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM