Using str_replace so that it only acts on the first match?

后端 未结 22 1368
醉酒成梦
醉酒成梦 2020-11-22 11:03

I want a version of str_replace() that only replaces the first occurrence of $search in the $subject. Is there an easy solution to thi

22条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-22 11:46

    $str = "Hello there folks!"
    $str_ex = explode("there, $str, 2);   //explodes $string just twice
                                          //outputs: array ("Hello ", " folks")
    $str_final = implode("", $str_ex);    // glues above array together
                                          // outputs: str("Hello  folks")
    

    There is one more additional space but it didnt matter as it was for backgound script in my case.

提交回复
热议问题