如何通过PHP重写文件的特定值?
通过PHP代码,文本文件中指定的“变量”应该更改其内容 新建文件.txt如何通过PHP重写文件的特定值?,php,variables,fwrite,Php,Variables,Fwrite,通过PHP代码,文本文件中指定的“变量”应该更改其内容 新建文件.txt <?php $info = "Hi"; $file = fopen("file.txt","w"); fwrite($file,$info); fclose($file); ?> $one = "first"; $two = "second"; $three = "third"; 如果我理解正确的话,您可以在file.txt文件中编写一些PHP代码。如果您只存储了变量,我相信您可以使用一些
<?php
$info = "Hi";
$file = fopen("file.txt","w");
fwrite($file,$info);
fclose($file);
?>
$one = "first";
$two = "second";
$three = "third";
如果我理解正确的话,您可以在file.txt文件中编写一些PHP代码。如果您只存储了变量,我相信您可以使用一些正则表达式来做您想做的事情 然而,对于任何更复杂的情况(甚至对于这个特定的情况),我建议您使用一些PHP解析器来解析所有变量及其值,并进行相应的更改。() 编辑:
只是为了澄清问题,简单的替换是不够的。假设您有类似于
$first=“a”
,然后您决定用\
替换a
。一个天真的替代者,会给你留下$first=“\”
在file.txt文件中。是的,您可以使用str_replace()将变量替换为您喜欢的数据
$one = "first";
$two = "hi";
$three = "third";
您能进一步解释一下吗?使用
include
(或require)并在include之后覆盖变量。也许您可以加载数组中的每一行制动器?然后您可以更改所需内容,以便重新创建文件
$s_input = 'this is a #first text';
$a_repfr = array('#first', '#second', '#third');
$a_repto = array('funny', 'php', 'love');
$s_output = str_replace($a_repfr, $a_repto, $s_input);