如何从字符串中删除 php 代码?
我有一个包含 php 代码的字符串,我需要从字符串中删除 php 代码,例如:
<?php $db1 = new ps_DB() ?><p>Dummy</p>
Should return Dummy
和一个没有 php 的字符串例如 Dummy
应返回相同的字符串。
我知道这可以用正则表达式来完成,但 4 小时后我还没有找到解决方案。
I have a string that has php code in it, I need to remove the php code from the string, for example:
<?php $db1 = new ps_DB() ?><p>Dummy</p>
Should return <p>Dummy</p>
And a string with no php for example <p>Dummy</p>
should return the same string.
I know this can be done with a regular expression, but after 4h I haven't found a solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
正如 ircmaxell 指出的:这需要有效的 PHP!
正则表达式路由将是(允许没有带短标签的“php”。字符串/文件中没有结尾?>(出于某种原因Zend推荐这样做?),当然还有UNgreedy & DOTALL模式:
As ircmaxell pointed out: this would require valid PHP!
A regex route would be (allowing for no 'php' with short tags. no ending ?> in the string / file (for some reason Zend recommends this?) and of course an UNgreedy & DOTALL pattern:
好吧,你可以使用 DomDocument 来做到这一点...
这两个函数将变成
编辑: 实际上,在查看 Wrikken 的答案后,我意识到这两种方法都有一个缺点...我的方法需要一些有效的 HTML标记(Dom 很不错,但它不会解析
foo)。 Wrikken 需要有效的 PHP(任何语法错误都会失败)。所以也许是两者的结合(先尝试一个。如果失败,再尝试另一个。如果两者都失败,那么如果不尝试找出它们失败的确切原因,你真的无能为力)......
Well, you can use DomDocument to do it...
Those two functions will turn
Edit: Actually, after looking at Wrikken's answer, I realized that both methods have a disadvantage... Mine requires somewhat valid HTML markup (Dom is decent, but it won't parse
<b>foo</b><?php echo $bar
). Wrikken's requires valid PHP (any syntax errors and it'll fail). So perhaps a combination of the two (try one first. If it fails, try the other. If both fail, there's really not much you can do without trying to figure out the exact reason they failed)...一个简单的解决方案是使用 php 标签分解为数组,删除数组之间的任何内容,然后分解回字符串。
这比正则表达式慢,但不需要有效的 html 或 php;它只需要关闭所有 php 标签。
对于并不总是包含最终结束标签的文件以及一般错误检查,您可以对标签进行计数,并在缺少结束标签时附加结束标签,或者在开始标签和结束标签未按预期添加时发出通知,例如添加代码下面在函数的开头。不过,这会减慢速度:)
A simple solution is to explode into arrays using the php tags to remove any content between and implode back to a string.
This is slower than regex but doesn't require valid html or php; it only requires all php tags to be closed.
For files which don't always include a final closing tag and for general error checking you could count the tags and append a closing tag if it's missing or notify if the opening and closing tags don't add up as expected, e.g. add the code below at the start of the function. This would slow it down a bit more though :)
这是 @jon 建议的 strip_php 的增强版本,它能够用另一个字符串替换代码的 php 部分:
This is an enhanced version of strip_php suggested by @jon that is able to replace php part of code with another string:
如果您使用 PHP,则只需使用正则表达式来替换与 PHP 代码匹配的任何内容。
以下语句将删除 PHP 标记:
如果没有找到任何匹配项,则不会替换任何内容。
If you are using PHP, you just need to use a regular expression to replace anything that matches PHP code.
The following statement will remove the PHP tag:
If it doesn't find any match, it won't replace anything.