【发布时间】:2022-01-23 13:41:51
【问题描述】:
我需要实现一个 preg_replace 来修复我在大量脚本中遇到的一些警告。
我的目标是替换...
$variable[key] = "WhatElse";
$result = $wso->RSLA("7050", $vegalot, "600", "WFID_OK_WEB","1300", $_POST[username]);
if ($result[ECD] != 0) {
if ($line=="AAAA" && in_array(substr($wso->lot,0,7),$lot_aaaa_list) && $lot[wafer][25]) {
... 相同的语句将 CONSTANTS 替换为 ARRAY KEYS ...
$variable['key'] = "WhatElse";
$result = $wso->RSLA("7050", $vegalot, "600", "WFID_OK_WEB","1300", $_POST['username']);
if ($result['ECD'] != 0) {
if ($line=="AAAA" && in_array(substr($wso->lot,0,7),$lot_aaaa_list) && $lot[wafer][25]) {
但不包括在字符串中声明数组变量的情况,即...
$output = "<input name='variable[key]' has to be preserved as it is.";
$output = 'Even this string variable[key] has to be preserved as it is.';
...因为它们将被替换(但这不是我想要的):
$output = "<input name='variable['key']' has to be preserved as it is.";
$output = 'Even this string variable['key'] has to be preserved as it is.';
每个语句都由“preg_match_all”语句标识,然后替换为“str_replace”:
preg_match_all('/(\[(\w*)\])/', $str, $matches, PREG_SET_ORDER, 0);
$replace_str = $str;
$local_changeflag = false;
foreach($matches as $m) {
if (!$m[2]) continue;
if (is_numeric($m[2])) continue;
$replace_str = str_replace($m[1], "['" . $m[2] . "']", $replace_str);
$local_changeflag = true;
}
您有什么建议可以更好地解决我遇到的此类问题吗?
【问题讨论】:
-
尝试like this demo 跳过引用的部分(不确定这个想法是否好)。
-
或者,this one,如果您只想匹配方括号内的有效标识符 (
'/(["\'])(?:(?=(\\\\?))\\2.)*?\\1(*SKIP)(*F)|(\[(?:[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)])/')。 -
$lot[wafer]中的晶圆不应该也被引用吗?
标签: php regex string replace preg-match-all