タグが常に入力ファイルの先頭にある場合<?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>