如何使用 preg_replace 将子字符串替换为数组中的字符串? (PHP)
How to replace substrings with strings from array using preg_replace? (PHP)
我有一个包含这些值的数据数组:
Array
(
[id] => 1
[myid] => 9
[date] => 2014-01-30
[user] => 17
[reason] => some text here...
)
这个字符串包含引用数据数组索引的数字:
$columns = '(0) as "id",(1) as "myid",(2) as "date",(3) as "user",(4) as "reason"';
是否可以将括号中的数字更改为数组中的适当值?
我知道如何使用 (string) preg_replace( (array) $patterns, (array) $replacements, (string) $subject)
但完全不知道如何解决这个问题。
理想情况下,结果字符串如下所示:
'1' as "id",'9' as "myid",'2014-01-30' as "date",'17' as "user",'some text here...' as "reason"
只需使用 str_replace 的循环。遍历数组,使用括号中的循环索引作为搜索字符串并将其替换为相应的值。
array_flip is your saviour
直接来自文档
<?php
$input = array("oranges", "apples", "pears");
$flipped = array_flip($input);
print_r($flipped);
?>
上面的例子会输出:
Array
(
[oranges] => 0
[apples] => 1
[pears] => 2
)
你可以用一个简单的 foreach
循环来完成:
$info = array(
'id' => 1,
'myid' => 9,
'date' => '2014-01-30',
'user' => 17,
'reason' => 'some text here...'
);
$columns = '(0) as "id",(1) as "myid",(2) as "date",(3) as "user",(4) as "reason"';
foreach (array_values($info) as $key => $value) {
$columns = str_replace(
'(' . $key . ')',
str_pad($value, strlen($value) + 2, "'", STR_PAD_BOTH),
$columns
);
}
echo $columns;
使用preg_replace_callback
和str_replace
函数的解决方案:
$columns = '(0) as "id",(1) as "myid",(2) as "date",(3) as "user",(4) as "reason"';
// $arr is your initial array
$result = preg_replace_callback(
"/(\(\d\)) as \"(\w+?)\",?/i",
function ($maches) use($arr){
return str_replace([$maches[1]], "'". $arr[$maches[2]]. "'", $maches[0]);
},
$columns);
print_r($result);
输出:
'1' as "id",'9' as "myid",'2014-01-30' as "date",'17' as "user",'some text here...' as "reason"
您只需使用一些 PHP 库函数即可。
$info = array(
'id' => 1,
'myid' => 9,
'date' => '2014-01-30',
'user' => 17,
'reason' => 'some text here...'
);
$columns = '(0) as "id",(1) as "myid",(2) as "date",(3) as "user",(4) as "reason"';
$new_Arr = array();
$column_arr = explode(",", $columns);
foreach($column_arr as $values){
$arr = explode(" as ", $values);
$new_Arr[] = "'".$info[trim($arr[1], '"')]."' as ".$arr[1];
}
echo implode(",", $new_Arr) //'1' as "id",'9' as "myid",'2014-01-30' as "date",'17' as "user",'some text here...' as "reason"
我有一个包含这些值的数据数组:
Array
(
[id] => 1
[myid] => 9
[date] => 2014-01-30
[user] => 17
[reason] => some text here...
)
这个字符串包含引用数据数组索引的数字:
$columns = '(0) as "id",(1) as "myid",(2) as "date",(3) as "user",(4) as "reason"';
是否可以将括号中的数字更改为数组中的适当值?
我知道如何使用 (string) preg_replace( (array) $patterns, (array) $replacements, (string) $subject)
但完全不知道如何解决这个问题。
理想情况下,结果字符串如下所示:
'1' as "id",'9' as "myid",'2014-01-30' as "date",'17' as "user",'some text here...' as "reason"
只需使用 str_replace 的循环。遍历数组,使用括号中的循环索引作为搜索字符串并将其替换为相应的值。
array_flip is your saviour
直接来自文档
<?php
$input = array("oranges", "apples", "pears");
$flipped = array_flip($input);
print_r($flipped);
?>
上面的例子会输出:
Array
(
[oranges] => 0
[apples] => 1
[pears] => 2
)
你可以用一个简单的 foreach
循环来完成:
$info = array(
'id' => 1,
'myid' => 9,
'date' => '2014-01-30',
'user' => 17,
'reason' => 'some text here...'
);
$columns = '(0) as "id",(1) as "myid",(2) as "date",(3) as "user",(4) as "reason"';
foreach (array_values($info) as $key => $value) {
$columns = str_replace(
'(' . $key . ')',
str_pad($value, strlen($value) + 2, "'", STR_PAD_BOTH),
$columns
);
}
echo $columns;
使用preg_replace_callback
和str_replace
函数的解决方案:
$columns = '(0) as "id",(1) as "myid",(2) as "date",(3) as "user",(4) as "reason"';
// $arr is your initial array
$result = preg_replace_callback(
"/(\(\d\)) as \"(\w+?)\",?/i",
function ($maches) use($arr){
return str_replace([$maches[1]], "'". $arr[$maches[2]]. "'", $maches[0]);
},
$columns);
print_r($result);
输出:
'1' as "id",'9' as "myid",'2014-01-30' as "date",'17' as "user",'some text here...' as "reason"
您只需使用一些 PHP 库函数即可。
$info = array(
'id' => 1,
'myid' => 9,
'date' => '2014-01-30',
'user' => 17,
'reason' => 'some text here...'
);
$columns = '(0) as "id",(1) as "myid",(2) as "date",(3) as "user",(4) as "reason"';
$new_Arr = array();
$column_arr = explode(",", $columns);
foreach($column_arr as $values){
$arr = explode(" as ", $values);
$new_Arr[] = "'".$info[trim($arr[1], '"')]."' as ".$arr[1];
}
echo implode(",", $new_Arr) //'1' as "id",'9' as "myid",'2014-01-30' as "date",'17' as "user",'some text here...' as "reason"