开发者

Ambiguity with str_replace case or replacement order

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.


Well, bigs does match big as well, doesn't it? Change the order so it checks if bigs matches first:

function replace_text($text) {
    $array = array(
    ':bigs' => 'BIIIGSS',
    ':big' => 'BIG',
    );


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 lowercase bigs is not present, and therefore not replaced.


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 now BIGs. Then your second replace is run, but now you cannot find the lower-case string bigs anymore.

str_replace replaces from left to right by the way, as the manual says.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜