简体   繁体   English

如何使用 preg_replace 和正则表达式替换 php 文件中的代码块?

[英]How can I replace a block of code in a php file using preg_replace and regex expression?

I have a php config file that contains a few arrays use to store settings.我有一个 php 配置文件,其中包含一些用于存储设置的数组。

<?php
$array1 = array(
    "item1" => "value1",
    "item2" => "value2",
    "item3" => "value3",
    "item4" => "value4",
);
$array2 = array(
    "item1",
    "item2",
    "item3",
    "item4",
);
$array3 = array(
    "item1" => "value1",
    "item2" => "value2",
    "item3" => "value3",
    "item4" => "value4",
);
?>

I am not very good with regex expression (yet).I am having diffulties understanding how it works.我对 regex 表达式不是很好(但是)。我很难理解它是如何工作的。 I am trying to build a regex expression that would allow me to replace all lines in array2.我正在尝试构建一个正则表达式,允许我替换 array2 中的所有行。 Using preg_replace($pattern, $replacement, $str) .使用preg_replace($pattern, $replacement, $str)

$str=file_get_contents('config.php');
$pattern = "/[$array2 = array(](.*)[);]/";
$replacement = '    "itemA",
    "itemB",
    "itemC",
    "itemD",';
$str=preg_replace($pattern, $replacement, $str);
file_put_contents('config.php', $str);

The expected result would be:预期结果将是:

<?php
$array1 = array(
    "item1" => "value1",
    "item2" => "value2",
    "item3" => "value3",
    "item4" => "value4",
);
$array2 = array(
    "itemA",
    "itemB",
    "itemC",
    "itemD",
);
$array3 = array(
    "item1" => "value1",
    "item2" => "value2",
    "item3" => "value3",
    "item4" => "value4",
);
?>

I guess, maybe some expression similar to我想,也许有些表达类似于

(\h{2,}"[^"\r\n]+")$

might work here to some level:可能在这里工作到某种程度:

RegEx Demo正则表达式演示

Test测试

$re = '/(\h{2,}"[^"\r\n]+")$/m';
$str = '$array1 = array(
    "item1\' => "value1",
    "item2\' => "value2",
    "item3\' => "value3",
    "item4\' => "value4",
);
$array2 = array(
    "item1"
    "item2"
    "item3"
    "item4"
);
$array3 = array(
    "item1\' => "value1",
    "item2\' => "value2",
    "item3\' => "value3",
    "item4\' => "value4",
);';
$subst = '$1,';

$result = preg_replace($re, $subst, $str);

echo $result;

Output输出

$array1 = array(
    "item1' => "value1",
    "item2' => "value2",
    "item3' => "value3",
    "item4' => "value4",
);
$array2 = array(
    "item1",
    "item2",
    "item3",
    "item4",
);
$array3 = array(
    "item1' => "value1",
    "item2' => "value2",
    "item3' => "value3",
    "item4' => "value4",
);

If you wish to simplify/update/explore the expression, it's been explained on the top right panel of regex101.com .如果您想简化/更新/探索该表达式,请在regex101.com的右上面板对其进行解释。 You can watch the matching steps or modify them in this debugger link , if you'd be interested.如果您有兴趣,可以在此调试器链接中观看匹配步骤或修改它们。 The debugger demonstrates that how a RegEx engine might step by step consume some sample input strings and would perform the matching process.调试器演示了 RegEx 引擎如何逐步使用一些示例输入字符串并执行匹配过程。


Method 2方法二

I guess, an expression similar to,我想,类似的表达,

(?s)(?<=\$array2 = array\()[^)]*

might be OK to look into.可能可以调查一下。

RegEx Demo 2正则表达式演示 2

$re = '/(?s)(?<=\$array2 = array\()[^)]*/';
$str = '$array1 = array(
    "item1" => "value1",
    "item2" => "value2",
    "item3" => "value3",
    "item4" => "value4",
);
$array2 = array(
    "item1",
    "item2",
    "item3",
    "item4",
);
$array3 = array(
    "item1" => "value1",
    "item2" => "value2",
    "item3" => "value3",
    "item4" => "value4",
);';
$subst = '\\n\\t"itemA",\\n\\t"itemB",\\n\\t"itemC",\\n\\t"itemD"\\n';

$result = preg_replace($re, $subst, $str);

echo $result;

Output 2输出 2

$array1 = array(
    "item1" => "value1",
    "item2" => "value2",
    "item3" => "value3",
    "item4" => "value4",
);
$array2 = array(\n\t"itemA",\n\t"itemB",\n\t"itemC",\n\t"itemD"\n);
$array3 = array(
    "item1" => "value1",
    "item2" => "value2",
    "item3" => "value3",
    "item4" => "value4",
);

Firstly thanks @Emma for all the help.首先感谢@Emma 的所有帮助。 Here is the final code that worked for me simplified with the same example.这是对我有用的最终代码,使用相同的示例进行了简化。

config.php配置.php

<?php
$array1 = array(
    "item1" => "value1",
    "item2" => "value2",
    "item3" => "value3",
    "item4" => "value4",
);
$array2 = array(
    "item1",
    "item2",
    "item3",
    "item4",
);
$array3 = array(
    "item1" => "value1",
    "item2" => "value2",
    "item3" => "value3",
    "item4" => "value4",
);
?>

PHP Code to manipulate the file:操作文件的 PHP 代码:

<?php
function format_array($newarray){
    return('    "'.$newarray.'",');
}
$pattern = '/(?s)(?<=\$array2 = array\()[^)]*/';
$myarray = array(
    "itemA",
    "itemB",
    "itemC",
    "itemD",
);
$myarray = array_map("format_array",$myarray);
array_unshift($myarray, '');
array_pop($myarray);
array_push($myarray,'');
$str=file_get_contents('config.php');
$str=preg_replace($pattern,implode(PHP_EOL, $myarray),$str);
file_put_contents('config.php', $str);
?>

Output输出

<?php
$array1 = array(
    "item1" => "value1",
    "item2" => "value2",
    "item3" => "value3",
    "item4" => "value4",
);
$array2 = array(
    "itemA",
    "itemB",
    "itemC",
    "itemD",
);
$array3 = array(
    "item1" => "value1",
    "item2" => "value2",
    "item3" => "value3",
    "item4" => "value4",
);
?>

In order to get the expected result including the indents, I had to store the new array and create a leading empty element along with another one at the end.为了获得包括缩进在内的预期结果,我必须存储新数组并在末尾创建一个前导空元素和另一个空元素。 This makes sure that newlines are created in the file so we keep the same formatting.这确保在文件中创建换行符,以便我们保持相同的格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM