Some times we need to display some numbers (like bank account number or password) in partial form. We can use following to functions to do it.
1. PHP function to replace a password with * alogn with some readable prefix (e.g. 5987***)
function hideRight($str,$prefixLen){
$strLen = strlen($str);
$passPrefix = substr($str,0,$prefixLen);
$newStr="";
for ($i=$prefixLen; $i<$strLen; $i++){
$newStr .="*";
}
$newStr = $passPrefix.$newStr;
return $newStr;
}
usage:
$input = "35468452";
$output = hideRight($input,'2');
//output will be 354684**
2. PHP function to replace a password with * alogn with some readable suffix (e.g. ****5987)
function hideLeft($str,$suffixLen){
$strLen = strlen($str);
$suffix = substr($str,-$suffixLen);
$newStr="";
for ($i=$suffixLen; $i<$strLen-1; $i++){
$newStr .="*";
}
$newStr = $newStr.$suffix;
return $newStr;
}
usage:
$input = "35468452";
$output = hideLeft($input,'2');
//output will be *****52
1. PHP function to replace a password with * alogn with some readable prefix (e.g. 5987***)
function hideRight($str,$prefixLen){
$strLen = strlen($str);
$passPrefix = substr($str,0,$prefixLen);
$newStr="";
for ($i=$prefixLen; $i<$strLen; $i++){
$newStr .="*";
}
$newStr = $passPrefix.$newStr;
return $newStr;
}
usage:
$input = "35468452";
$output = hideRight($input,'2');
//output will be 354684**
2. PHP function to replace a password with * alogn with some readable suffix (e.g. ****5987)
function hideLeft($str,$suffixLen){
$strLen = strlen($str);
$suffix = substr($str,-$suffixLen);
$newStr="";
for ($i=$suffixLen; $i<$strLen-1; $i++){
$newStr .="*";
}
$newStr = $newStr.$suffix;
return $newStr;
}
usage:
$input = "35468452";
$output = hideLeft($input,'2');
//output will be *****52
No comments:
Post a Comment