php str_ireplace without losing case

后端 未结 3 932
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-18 06:53

is it possible to run str_ireplace without it destroying the original casing?

For instance:

$txt = \"Hello How Are You\";
$a = \"are\";
$h = \"hello\         


        
3条回答
  •  一生所求
    2020-12-18 07:01

    Not beautiful, but should work.

    function str_replace_alt($search,$replace,$string)
    {
        $uppercase_search = strtoupper($search);
        $titleCase_search = ucwords($search);
        $lowercase_replace = strtolower($replace);
        $uppercase_replace = strtoupper($replace);
        $titleCase_replace = ucwords($replace);
    
        $string = str_replace($uppercase_search,$uppercase_replace,$string);
        $string = str_replace($titleCase_search,$titleCase_replace,$string);
        $string = str_ireplace($search,$lowercase_replace,$string);
    
        return $string;
    }
    

提交回复
热议问题