正则表达式帮助(PHP、preg_替换)
我需要对字符串中的所有PHP标记以及PHP标记之间的任何字符进行preg_替换 例如,如果文件内容为:正则表达式帮助(PHP、preg_替换),php,regex,preg-replace,Php,Regex,Preg Replace,我需要对字符串中的所有PHP标记以及PHP标记之间的任何字符进行preg_替换 例如,如果文件内容为: Hey there! <?php some_stuff() ?> Woohoo! 这是我的密码: $file_contents = file_get_contents('somefilename.php'); $regex = '#([<?php](.*)[\?>])#e'; $file_contents = preg_replace($regex, '<<
Hey there!
<?php some_stuff() ?>
Woohoo!
这是我的密码:
$file_contents = file_get_contents('somefilename.php');
$regex = '#([<?php](.*)[\?>])#e';
$file_contents = preg_replace($regex, '<<GENERATED CONTENT>>', $file_contents);
$file\u contents=file\u get\u contents('somefilename.php');
$regex='#([])#e';
$file\u contents=preg\u replace($regex,,$file\u contents);
失败
我的正则表达式技能很差,有人能帮我修一下正则表达式吗。谢谢。试试这个正则表达式:
#<\?.*?\?>#
##
应该也适用于短标记(不带'php')
我认为你尝试的主要问题是,你需要用反斜杠避开问号,并且你在不应该使用方括号的地方使用了方括号。方括号表示“选择这些字符中的任意一个”。您可以尝试:
$regex = '#<\?php.*?\?>#i';
$regex='##i';
使用的正则表达式:
$regex=“/为作业使用正确的工具。包含从周围内容中剥离PHP代码所需的所有功能: 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一些HTML 更多HTML 那好吧
<?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]); } }
您需要用反斜杠避开第一个和最后一个问号。这也会起作用,是一个很好的解释。这需要
<p>Some HTML</p> <?php echo("hello world"); ?> <p>More HTML</p> <?php /* Strip this out please */ ?> <p>Ok Then</p>
<?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]); } }
<p>Some HTML</p> <p>More HTML</p> <p>Ok Then</p>