考虑下面的代码...
${"transformation_" . $image_selector}[0] = 'translated to the left and down,';
$trans_1 = str_replace("left", "right", ${"transformation_" . $image_selector}[0]);
$trans_1 = str_replace("right", "left", ${"transformation_" . $image_selector}[0]);
$trans_1 = str_replace("up", "down", ${"transformation_" . $image_selector}[0]);
$trans_1 = str_replace("down", "up", ${"transformation_" . $image_selector}[0]);
echo $trans_1;
我正在尝试将“ left”替换为“ right”(反之亦然),将“ up”替换为“ down”(反之亦然)。在echo
不断出来为平移到左侧和最多的时候,应该翻译成right
及以上
有任何想法吗?
您总是将原始字符串传递给str_replace()
并覆盖存储结果的相同变量。因此,只有最后一次替换发生。
${"transformation_" . $image_selector}[0] = 'translated to the left and down,';
$trans_1 = str_replace("left", "right", ${"transformation_" . $image_selector}[0]);
$trans_1 = str_replace("right", "left", ${"transformation_" . $image_selector}[0]);
$trans_1 = str_replace("up", "down", ${"transformation_" . $image_selector}[0]);
$trans_1 = str_replace("down", "up", ${"transformation_" . $image_selector}[0]); // only this is really performed, the rest is overwritten
echo $trans_1;
尝试
echo strtr(${"transformation_" . $image_selector}[0], array(
'left' => 'right',
'right' => 'left',
'up' => 'down',
'down' => 'up'
));
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句