replace last seven characters of string

前端 未结 2 1116
萌比男神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:14
    $cuttedString = substr("your string", -7);
    

    this should do the job.

    0 讨论(0)
  • 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);
    };
    
    0 讨论(0)
提交回复
热议问题