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

后端 未结 22 1195
醉酒成梦
醉酒成梦 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:40

    $str = "/property/details&id=202&test=123#tab-6p";
    $position = strpos($str,"&");
    echo substr_replace($str,"?",$position,1);
    

    Using substr_replace we can replace the occurrence of first character only in string. as & is repeated multiple times but only at first position we have to replace & with ?

提交回复
热议问题