str_replace: Match whole word only

前端 未结 5 1169
后悔当初
后悔当初 2021-01-18 13:21

Since str_replace() matches \":Name\" two times in \":Name :Name_en\" I want to match the results for the whole word only. I wanted to switch to preg_repl

5条回答
  •  庸人自扰
    2021-01-18 14:21

    If your using PHP 5+ you can still use str_replace.

    $str = ":Name :Name_en";
    echo $str . chr(10);
    
    // The final int limits the function to a single replace.
    $str = str_replace(':Name', '"Test"', $str, 1);
    
    echo $str;
    

提交回复
热议问题