Dom*_*nes 12
如果您不需要使用PHP,我强烈建议您从命令行执行这样的操作.它是最好的工具,更容易使用.
在任何情况下,sed(流编辑器)命令都是您正在寻找的:
sed s/search/replace oldfilename > newfilename
Run Code Online (Sandbox Code Playgroud)
如果您需要不区分大小写:
sed -i s/search/replace oldfilename > newfilename
Run Code Online (Sandbox Code Playgroud)
如果您需要在PHP中动态执行此操作,您可以使用passthru():
$output = passthru("sed s/$search/$replace $oldfilename > $newfilename");
Run Code Online (Sandbox Code Playgroud)
干得好:
function replace_file($path, $string, $replace)
{
set_time_limit(0);
if (is_file($path) === true)
{
$file = fopen($path, 'r');
$temp = tempnam('./', 'tmp');
if (is_resource($file) === true)
{
while (feof($file) === false)
{
file_put_contents($temp, str_replace($string, $replace, fgets($file)), FILE_APPEND);
}
fclose($file);
}
unlink($path);
}
return rename($temp, $path);
}
Run Code Online (Sandbox Code Playgroud)
像这样称呼它:
replace_file('/path/to/fruits.txt', 'apples', 'oranges');
Run Code Online (Sandbox Code Playgroud)
如果您不能直接从命令行使用 sed,因为它是一个动态任务,并且您需要从 php 调用它,那么很难获得正确的语法:您必须在搜索和替换字符串中以不同的方式转义这些字符
' / $ . * [ ] \ ^ &
Run Code Online (Sandbox Code Playgroud)
以下函数搜索并替换文件中的字符串,而不将搜索到的字符串解释为正则表达式。因此,如果您愿意,可以搜索字符串“.*”并将其替换为“$”。
/**
* str_replace_with_sed($search, $replace, $file_in, $file_out=null)
*
* Search for the fixed string `$search` inside the file `$file_in`
* and replace it with `$replace`. The replace occurs in-place unless
* `$file_out` is defined: in that case the resulting file is written
* into `$file_out`
*
* Return: sed return status (0 means success, any other integer failure)
*/
function str_replace_with_sed($search, $replace, $file_in, $file_out=null)
{
$cmd_opts = '';
if (! $file_out)
{
// replace inline in $file_in
$cmd_opts .= ' -i';
}
// We will use Basic Regular Expressions (BRE). This means that in the
// search pattern we must escape
// $.*[\]^
//
// The replacement string must have these characters escaped
// \ &
//
// In both cases we must escape the separator character too ( usually / )
//
// Since we run the command trough the shell we We must escape the string
// too (yai!). We're delimiting the string with single quotes (') and we'll
// escape them with '\'' (close string, write a single quote, reopen string)
// Replace all the backslashes as first thing. If we do it in the following
// batch replace we would end up with bogus results
$search_pattern = str_replace('\\', '\\\\', $search);
$search_pattern = str_replace(array('$', '.', '*', '[', ']', '^'),
array('\\$', '\\.', '\\*', '\\[', '\\]', '\\^'),
$search_pattern);
$replace_string = str_replace(array('\\', '&'),
array('\\\\', '\\&'),
$replace);
$output_suffix = $file_out ? " > '$file_out' " : '';
$cmd = sprintf("sed ".$cmd_opts." -e 's/%s/%s/g' \"%s\" ".$output_suffix,
str_replace('/','\\/', # escape the regexp separator
str_replace("'", "'\''", $search_pattern) // sh string escape
),
str_replace('/','\\/', # escape the regexp separator
str_replace("'", "'\''", $replace_string) // sh string escape
),
$file_in
);
passthru($cmd, $status);
return $status;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14748 次 |
| 最近记录: |