gpt4 book ai didi

php - 如何在 str_replace() 的搜索参数中获取数组中的数值以按照我期望的方式进行映射?

转载 作者:搜寻专家 更新时间:2023-10-31 20:42:05 27 4
gpt4 key购买 nike

php > echo str_replace(
array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'),
array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p'),
'01234567890abcdef') . PHP_EOL;
klmnopghijkklmnop

我希望这个 str_replace() 调用能给我类似 abcdefghijklmnop 的东西。但似乎我的搜索数组中的数字字符串被视为字符串以外的东西。

我最终只是追求 tr(1) 的功能(例如,tr a-f k-p | tr 0-9 a-j),当然,使用纯 php 而不是 shellout。

我在 php-5.4.10--pl0-gentoo 和 php-5.5.4-pl0-gentoo 上看到了同样的行为,所以我认为这不仅仅是一个 PHP 版本的侥幸。为什么 str_replace() 会奇怪地对待搜索数组中的数字——它是如何对待它们的?

最佳答案

您可以在 PHP 中使用 strtr 函数:

echo strtr( '01234567890abcdef', '0123456789abcdef', 'abcdefghijklmnop' ) . PHP_EOL;
// abcdefghijaklmnop

关于php - 如何在 str_replace() 的搜索参数中获取数组中的数值以按照我期望的方式进行映射?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19783865/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com