Replace string only once with php preg_replace

后端 未结 2 1206
长情又很酷
长情又很酷 2020-12-02 02:44

I need a replace string once function and believe preg_match might be my best bet.

I was using this, but due to the dynamicness of use, sometimes this function behav

相关标签:
2条回答
  • 2020-12-02 02:51

    You can also use T-Regx library:

    pattern('[a-z]+')->replace($string)->first()->with($replace);
    

    and also you should not use preg_quote(), as it's not safe - try Prepared Patterns.

    0 讨论(0)
  • 2020-12-02 02:58

    You are looking for preg_quote instead of trying to escape the \ yourself (which doesn't take [, + and many others into account):

    $return = preg_replace('/'.preg_quote($remove,'/').'/', $replace, $string, 1);
    
    0 讨论(0)
提交回复
热议问题