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 "#######". So, the final string will be "MerryChristmasu#######".
I have tried the follow code but it returns "MerryChristmasu#######1". It does not convert all seven ending characters.
$string = "MerryChristmasu87yujh7";
$match = substr($string, -7, -1);
$result = str_replace($match, "#######", $string);
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);
};
$cuttedString = substr("your string", -7);
this should do the job.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With