0

ファイルをファイルに書き込みたいのですが、ファイルに 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>
4

3 に答える 3

7

最も簡単な方法は、おそらく を使用してファイルを解析しtoken_get_all、結果をループして、タイプではないものをすべて破棄することT_INLINE_HTMLです。

于 2011-11-24T23:27:36.147 に答える
1

タグが常に入力ファイルの先頭にある場合<?php ?>は、入力を展開して、タグの周りのすべてを出力に書き込むことができます。

入力:

<?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>

ただし、複数の<?php ?>タグ セットを使用する予定がある場合は、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>
于 2011-11-25T01:52:08.323 に答える
1

書き込み先のファイル名を選択できる場合は、PHP として評価されない .phps ファイルに書き込むことができます。訪問者が .phps ページを表示すると、<?php ?>タグ内のすべてと HTML を含むプレーンテキスト ファイルが提供されます。

于 2011-11-24T23:29:04.200 に答える