str_replace 大小写或替换顺序不明确
str_replace 遇到奇怪的问题。
这是我的代码:
function replace_text($text) {
$array = array(
':big' => 'BIG',
':bigs' => 'BIIIGSS',
);
问题是当我输入 bigs
(带有 s)时,代码仅将文本转换为 BIGs
而不是 BIIIGSS
。
Having strange problem with str_replace.
Here's my code:
function replace_text($text) {
$array = array(
':big' => 'BIG',
':bigs' => 'BIIIGSS',
);
The problem is when i enter bigs
(with s) the code only turn the text to BIGs
not BIIIGSS
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
嗯,大个子也适合大个子,不是吗?更改顺序,以便首先检查大牌是否匹配:
Well, bigs does match big as well, doesn't it? Change the order so it checks if bigs matches first:
尝试使用不区分大小写的
str_ireplace()
反而。发生的情况是,当它到达数组中的第二个元素时,该值为
BIGs
,因此小写的bigs
不存在,因此不会被替换。Try using the case insensitive
str_ireplace()
instead.What happens is by the time it gets to the second element in the array, the value is
BIGs
, so lowercasebigs
is not present, and therefore not replaced.我怀疑这是您的完整代码,而只是对多次调用
str_replace
的函数的函数调用?如果是这样,那么您可能首先进行第一次替换,因此您的字符串
bigs
现在是BIGs
。然后运行第二次替换,但现在您再也找不到小写字符串bigs
了。顺便说一下,str_replace 从左到右替换,正如手册所说。
I doubt that this is your complete code, but just a function call to a function that calls
str_replace
multiple times?If so, then you are probably first doing the first replace, so your string
bigs
is nowBIGs
. Then your second replace is run, but now you cannot find the lower-case stringbigs
anymore.str_replace replaces from left to right by the way, as the manual says.