[英]How can I replace a block of code in a php file using preg_replace and regex expression?
我有一個 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",
);
?>
我對 regex 表達式不是很好(但是)。我很難理解它是如何工作的。 我正在嘗試構建一個正則表達式,允許我替換 array2 中的所有行。 使用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);
預期結果將是:
<?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",
);
?>
我想,也許有些表達類似於
(\h{2,}"[^"\r\n]+")$
可能在這里工作到某種程度:
$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;
$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",
);
如果您想簡化/更新/探索該表達式,請在regex101.com的右上面板對其進行解釋。 如果您有興趣,可以在此調試器鏈接中觀看匹配步驟或修改它們。 調試器演示了 RegEx 引擎如何逐步使用一些示例輸入字符串並執行匹配過程。
我想,類似的表達,
(?s)(?<=\$array2 = array\()[^)]*
可能可以調查一下。
$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;
$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",
);
首先感謝@Emma 的所有幫助。 這是對我有用的最終代碼,使用相同的示例進行了簡化。
配置.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 代碼:
<?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);
?>
輸出
<?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",
);
?>
為了獲得包括縮進在內的預期結果,我必須存儲新數組並在末尾創建一個前導空元素和另一個空元素。 這確保在文件中創建換行符,以便我們保持相同的格式。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.