0

I'm developing an app that i need to show an string to user that's filtered with * !

let me explain it more!

we have "Password" as string that user will see with 8 letter length! so i should filter 80% of this word then show the result like this : "pa*****d"

how can i do it with PHP?

miss nefrat
  • 93
  • 1
  • 1
  • 9
  • I'm hoping this is an example, you should never do that with actual passwords – maxpelic Mar 29 '19 at 03:18
  • why? i encrypt passwords with AES algorythm and save it to database. in some cases user most see this mode of password! is there any way to replace? – miss nefrat Mar 29 '19 at 03:30
  • Check out [this post](https://stackoverflow.com/q/326699/9530226) – maxpelic Mar 29 '19 at 03:34
  • Basically if you encrypt the passwords and someone gets the key, you compromise all of your users data. A good hash and salt combination is impossible to get the password from – maxpelic Mar 29 '19 at 03:38
  • @maxpelic ok thank you! does your website is up? :( – miss nefrat Mar 29 '19 at 03:39
  • I'm not sure what you're asking? – maxpelic Mar 29 '19 at 03:43
  • showing **anything** from the password is very bad idea. By showing `"pa*****d"` you instantly weakened this password. In this case from 8 you cut it down to 5 only. What is that really for? – Marcin Orlowski Mar 29 '19 at 03:46
  • @MarcinOrlowski this is for an api bot , users will auth. by telegram User ID and i doesn't need high security in this case! this password is just a way to enter their account safely , no body can't use it on another telegram account! – miss nefrat Mar 29 '19 at 03:50

1 Answers1

0

Simple like this:

$string = "password";

$mask_string =  substr($string,0, 4) . str_repeat("*", strlen($string)-5) . substr($string, -1);

echo $mask_string;

----Edit-----

If you want to use percentage this could be like this:

$percent = 0.80; //80%
$string = "password";
$cut = round(strlen($string)*$percent);
$show = strlen($string)-$cut;

$mask_string =  substr($string,0, $show) . str_repeat("*", $cut-1) . substr($string, -1);

echo $mask_string;
Snickfire
  • 502
  • 1
  • 3
  • 18