replace last seven characters of string

前端 未结 2 1115
萌比男神i
萌比男神i 2021-01-20 05:57

I have a string and I want to replace the last 7 charators of the string with \"#\". For example I have \"MerryChristmasu87yujh7\" I want to replace \"87yujh7\" with seven \

2条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-20 06:18

    Should be...

    $match = substr($string, -7);
    

    ... without the final -1. But in fact, it's far better done with...

    $result = substr($string, 0, -7) . str_repeat('#', 7);
    

    ... or, more generic:

    $coverWith = function($string, $char, $number) {
      return substr($string, 0, -$number) . str_repeat($char, $number);
    };
    

提交回复
热议问题