【发布时间】:2015-11-03 15:07:29
【问题描述】:
这是我的数组:
$class = $row["class"];
$classes = array( '1', '2', '4', '5', '6', '7', '8', '9', '10', '11'
);
$replacements = array( 'Warrior', 'Paladin', 'Hunter', 'Rogue',
'Priest', 'Death Knight', 'Shaman', 'Mage', 'Warlock', 'Monk',
'Druid' );
$resultclass = str_replace( $classes, $replacements, $class );
我的问题是,当我从数据库中获取数字 11 时,它会显示两次“战士”而不是“德鲁伊”
我该如何解决?
【问题讨论】:
-
$class 中有什么?数据库到底在哪里?这些只是数组...
-
$resultclass = strtr($class , $classes, $replacements); -
不是最好的,但是如果你的类不会变得更多,你可以在你的 $classes 数组中切换
1和11,所以首先它将替换 11 和更高版本1 xD -
阅读PHP Docs,它有一个关于str_replace
Replacement order gotcha Because str_replace() replaces left to right, it might replace a previously inserted value when doing multiple replacements. See also the examples in this document.的大警告 -
看看这个问答——可以使用preg_replace:stackoverflow.com/questions/9416175/…