PHP 写入文件减去 PHP 代码?

发布于 2024-12-18 02:14:27 字数 382 浏览 0 评论 0原文

我想将一个文件写入一个文件,该文件包含一些 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

忆离笙 2024-12-25 02:14:27

最简单的方法可能是使用 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 type T_INLINE_HTML.

夏日落 2024-12-25 02:14:27

如果您的 标签始终位于输入文件的顶部,您可以分解输入并将标签周围的所有内容写入输出:

输入:

<?php echo "This is the PHP I want removed!"; ?>
<html> 
    <p>This is what I want written to a file!</p> 
</html>

代码:

$inputTxt = file_get_contents($path . $file , NULL, NULL);

$begin = explode("<?php", $inputTxt);
$end = explode('?>', $inputTxt);
fwrite($output,  $begin[0] . $end[1] . "\n\n");
?>

输出:

之前

<?php
echo "This is the PHP I want removed!";
?>
<html>
<p>This is what I want written to a file!</p>
</html>

之后

<html>
<p>This is what I want written to a file!</p>
</html>

但是,如果您计划拥有多于一组 标记,那么您需要使用 preg_match:

输入:

<?php
echo "This is the PHP I want removed!";
?>
<html>
    <p>This is <?php echo $something; ?> I want written to a file!</p>
</html>

代码:

<?php
$file="input.txt";
$path='C:\\input\\';
$output = fopen($path . "output.txt",'w');

$inputTxt = file_get_contents($path . $file , NULL, NULL);

$pattern = '/<\?php.+\?>/isU';
$replace = '';

$newInput = preg_replace($pattern, $replace, $inputTxt);

fwrite($output,  $newInput);
?>

输出:

之前

<?php
echo "This is the PHP I want removed!";
?>
<html>
<p>This is <?php echo $something; ?> I want written to a file!</p>
</html>

之后

<html>
<p>This is  I want written to a file!</p>
</html>

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:

<?php echo "This is the PHP I want removed!"; ?>
<html> 
    <p>This is what I want written to a file!</p> 
</html>

Code:

$inputTxt = file_get_contents($path . $file , NULL, NULL);

$begin = explode("<?php", $inputTxt);
$end = explode('?>', $inputTxt);
fwrite($output,  $begin[0] . $end[1] . "\n\n");
?>

Output:

Before

<?php
echo "This is the PHP I want removed!";
?>
<html>
<p>This is what I want written to a file!</p>
</html>

After

<html>
<p>This is what I want written to a file!</p>
</html>

But, if you plan on having more than one set of <?php ?> tags, then you would need to use preg_match:

Input:

<?php
echo "This is the PHP I want removed!";
?>
<html>
    <p>This is <?php echo $something; ?> I want written to a file!</p>
</html>

Code:

<?php
$file="input.txt";
$path='C:\\input\\';
$output = fopen($path . "output.txt",'w');

$inputTxt = file_get_contents($path . $file , NULL, NULL);

$pattern = '/<\?php.+\?>/isU';
$replace = '';

$newInput = preg_replace($pattern, $replace, $inputTxt);

fwrite($output,  $newInput);
?>

Output:

Before

<?php
echo "This is the PHP I want removed!";
?>
<html>
<p>This is <?php echo $something; ?> I want written to a file!</p>
</html>

After

<html>
<p>This is  I want written to a file!</p>
</html>
落叶缤纷 2024-12-25 02:14:27

如果您可以选择要写入的文件名,则可以写入 .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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文