Trying to replace parts of string start with same search chars

后端 未结 3 2059
北海茫月
北海茫月 2020-12-02 03:19

I\'m trying to replace parts of my string. But I met a problem when my search string start with same character:

$string = \"Good one :y. Keep going :y2\"; 

         


        
3条回答
  •  一生所求
    2020-12-02 03:36

    :y\b
    

    Use this to replace only :y and not :y2.See demo.

    https://regex101.com/r/sJ9gM7/9

    $re = "":y\\b"m";
    $str = "Good one :y. Keep going :y2\n";
    $subst = "a";
    
    
    $result = preg_replace($re, $subst, $str);
    

    Similarly for :y2 use :y2\b.

提交回复
热议问题