PHP 写入文件减去 PHP 代码?
我想将一个文件写入一个文件,该文件包含一些 PHP 代码。我不希望当有人读取该文件时该文件运行 PHP。基本上,我想要 和
?>
之间的所有文本以及这些标签。有没有办法在 PHP 中做到这一点?可能与 strpos
一起使用吗?我尝试使用 strpos;但我不明白。
这是一个例子:
<?php
echo "This is the PHP I want removed!";
?>
<html>
<p>This is what I want written to a file!</p>
</html>
I want to write a file to a file and the file contains some PHP code. I don't want the file to run the PHP when someone reads the file. Basically, I want all the text between the <?php
and the ?>
, plus those tags. Is there any way to do this in PHP? Possibly with strpos
? I tried to use strpos; but I couldn't figure it out.
Here's an example:
<?php
echo "This is the PHP I want removed!";
?>
<html>
<p>This is what I want written to a file!</p>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最简单的方法可能是使用
token_get_all
解析文件,循环遍历结果并丢弃所有不属于T_INLINE_HTML
类型的内容。The easiest way is probably to parse the file using
token_get_all
, loop through the result and discard everything that's not of typeT_INLINE_HTML
.如果您的
标签始终位于输入文件的顶部,您可以分解输入并将标签周围的所有内容写入输出:
输入:
代码:
输出:
之前
之后
但是,如果您计划拥有多于一组
标记,那么您需要使用 preg_match:
输入:
代码:
输出:
之前
之后
If your
<?php ?>
tags are always gonna me at the top of your input file, you could just explode the input and write to your output everything around your tags:Input:
Code:
Output:
Before
After
But, if you plan on having more than one set of
<?php ?>
tags, then you would need to use preg_match:Input:
Code:
Output:
Before
After
如果您可以选择要写入的文件名,则可以写入 .phps 文件,该文件不会被评估为 PHP。如果访问者查看 .phps 页面,他们将获得一个纯文本文件,其中包含
标记内的所有内容以及 HTML。
If you can choose the filename you're writing to, you can write to a .phps file, which won't be evaluated as PHP. If a visitor views the .phps page, they'll be served up a plaintext file that includes everything inside the
<?php ?>
tags, as well as the HTML.