更新时间:2023-02-23 17:34:38
您不需要正则表达式.请尝试以下操作:
You don't need a regex for this. Try the following:
file()
将文件加载到数组中并循环遍历各行2_
$word
并将其连接到 $result
字符串$result
字符串file_get_contents()
并将结果写入文件file()
and loop through the lines2_
$word
and concatenate it to the $result
string$result
stringfile_get_contents()
and write the result to the file代码:
$lines = file('file.txt');
$word = 'word';
$result = '';
foreach($lines as $line) {
if(substr($line, 0, 2) == '2_') {
$result .= '2_'.$word."\n";
} else {
$result .= $line;
}
}
file_put_contents('file.txt', $result);
现在,如果替换发生,那么 file.txt
将包含如下内容:
Now, if the replace took place, then file.txt
would contain the something like:
1_fjd
2_word
3_fks