如何用 PHP 中 2 个数组中的数据替换字符串?
How to replace a string with data, from 2 arrays, in PHP?
我有一个简单的字符串:$StrOne = "There is my text.";
。而且,我也有 2 个简单数组:
$ArrOne = array (
"1" => "a",
"7" => "e",
"5" => "c",
"4" => "x",
"2" => "r"
);
-
$ArrTwo = array (
"7" => "k",
"9" => "z",
"1" => "y",
"3" => "x",
"2" => "b"
);
我想用 $ArrOne
中的值替换 $StrOne
,用 $ArrTwo
中的值替换。有我的想法:
- 第一步,如果
$StrOne
中有组字母($ArrOne
中存在的),则在$ArrTwo
中查找同一个键附加了什么值($ArrOne
中存在的值)。
- 第 2 步。用此函数替换所有现有值(来自 第 1 步。):
strtr();
。
- 第 3 步。显示新的输出字符串(已完全替换,来自 第 2 步。)。
输出字符串为:$StrTwo = "Thkrk is my tkxt.";
.
如何创建一个简单的 PHP 函数,它会像这样执行?
首先我们需要使用 array_intersect_key
来查找同时出现在两个数组中的键。
其次,我们对相交的数组进行排序,使它们的键与 ksort
的顺序相同。
第三,我们使用 array_combine
为 strtr
函数创建第二个参数。
第四我们使用strtr
:
$arrayOne = array (
"1" => "a",
"7" => "e",
"5" => "c",
"4" => "x",
"2" => "r"
);
$arrayTwo = array (
"7" => "k",
"9" => "z",
"1" => "y",
"3" => "x",
"2" => "b"
);
$keys = array_intersect_key($arrayOne, $arrayTwo);
ksort($keys);
$values = array_intersect_key($arrayTwo, $arrayOne);
ksort($values);
echo strtr('There is my text.', array_combine($keys, $values));
更新:对于旧版本尝试:
$replace = array();
foreach ($arrayOne as $k => $v) {
// use isset if you want to replace something with empty string
if (!empty($arrayTwo[$k])) {
$replace[$v] = $arrayTwo[$k];
}
}
echo strtr('There is my text.', $replace);
您提到您不能使用 array_intersect
。我会这样做:
//remove anything from $arrayOne that doesn't have a replacement
// in $arrayTwo
$arrayOne = array_filter(
$arrayOne,
function($k) use($arrayTwo){return isset($arrayTwo[$k]);},
ARRAY_FILTER_USE_KEY);
//remove anything from $arrayTwo that doesn't have a match
//in $arraaOne
$arrayTwo = array_filter(
$arrayTwo,
function($k) use($arrayOne){return isset($arrayOne[$k]);},
ARRAY_FILTER_USE_KEY);
//sort the keys so that matching keys will be in same position
ksort($arrayOne); ksort($arrayTwo);
//Now arrays have the same keys in the same position.
//It's safe to perform the replacement
$stringOut = str_replace($arrayOne,$arrayTwo,$stringInp);
我有一个简单的字符串:$StrOne = "There is my text.";
。而且,我也有 2 个简单数组:
$ArrOne = array (
"1" => "a",
"7" => "e",
"5" => "c",
"4" => "x",
"2" => "r"
);
-
$ArrTwo = array (
"7" => "k",
"9" => "z",
"1" => "y",
"3" => "x",
"2" => "b"
);
我想用 $ArrOne
中的值替换 $StrOne
,用 $ArrTwo
中的值替换。有我的想法:
- 第一步,如果
$StrOne
中有组字母($ArrOne
中存在的),则在$ArrTwo
中查找同一个键附加了什么值($ArrOne
中存在的值)。 - 第 2 步。用此函数替换所有现有值(来自 第 1 步。):
strtr();
。 - 第 3 步。显示新的输出字符串(已完全替换,来自 第 2 步。)。
输出字符串为:$StrTwo = "Thkrk is my tkxt.";
.
如何创建一个简单的 PHP 函数,它会像这样执行?
首先我们需要使用 array_intersect_key
来查找同时出现在两个数组中的键。
其次,我们对相交的数组进行排序,使它们的键与 ksort
的顺序相同。
第三,我们使用 array_combine
为 strtr
函数创建第二个参数。
第四我们使用strtr
:
$arrayOne = array (
"1" => "a",
"7" => "e",
"5" => "c",
"4" => "x",
"2" => "r"
);
$arrayTwo = array (
"7" => "k",
"9" => "z",
"1" => "y",
"3" => "x",
"2" => "b"
);
$keys = array_intersect_key($arrayOne, $arrayTwo);
ksort($keys);
$values = array_intersect_key($arrayTwo, $arrayOne);
ksort($values);
echo strtr('There is my text.', array_combine($keys, $values));
更新:对于旧版本尝试:
$replace = array();
foreach ($arrayOne as $k => $v) {
// use isset if you want to replace something with empty string
if (!empty($arrayTwo[$k])) {
$replace[$v] = $arrayTwo[$k];
}
}
echo strtr('There is my text.', $replace);
您提到您不能使用 array_intersect
。我会这样做:
//remove anything from $arrayOne that doesn't have a replacement
// in $arrayTwo
$arrayOne = array_filter(
$arrayOne,
function($k) use($arrayTwo){return isset($arrayTwo[$k]);},
ARRAY_FILTER_USE_KEY);
//remove anything from $arrayTwo that doesn't have a match
//in $arraaOne
$arrayTwo = array_filter(
$arrayTwo,
function($k) use($arrayOne){return isset($arrayOne[$k]);},
ARRAY_FILTER_USE_KEY);
//sort the keys so that matching keys will be in same position
ksort($arrayOne); ksort($arrayTwo);
//Now arrays have the same keys in the same position.
//It's safe to perform the replacement
$stringOut = str_replace($arrayOne,$arrayTwo,$stringInp);