Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace all characters of string to asterisks except first and last characters in PHP [duplicate]

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

like image 459
Michael Samuel Avatar asked Nov 30 '22 13:11

Michael Samuel


1 Answers

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 
like image 83
Jage Avatar answered Dec 05 '22 22:12

Jage