How to replace all characters of string to asterisks except first and last characters in PHP?
For example test
should become t**t
and profanity
become p******y
and so on
function get_starred($str) {
$len = strlen($str);
return substr($str, 0, 1).str_repeat('*', $len - 2).substr($str, $len - 1, 1);
}
$myStr = 'YourName';
echo get_starred($myStr); //should show Y******e
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