replace letters with *

using php

could you only display first two letters of a name and the other letters *

sarah
sa***
james
ja***
mary
ma**
LVL 1
rgb192Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Amar BardoliwalaCommented:
Hello rgb192,

Following script should help you.

<?php
function show2chars($string)
{
	$str = substr($string,0,2);
	
	$str = str_pad($str,strlen($string),'*', STR_PAD_RIGHT);
	
	return $str;
}	

print "<br/>Sarah => " . show2chars("Sarah");
print "<br/>James => " . show2chars("James");
print "<br/>Mary => " . show2chars("Mary");
?>

Open in new window


Thank you.

Amar Bardoliwala
0
käµfm³d 👽Commented:
Since you tagged this as regex...

$result = preg_replace('/(?<!^.|^)./', "*", $input);

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
0
rgb192Author Commented:
I had a question last week about doing this with an email but not hiding the domain name of the email

I either regex or substring work

Thank you
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Regular Expressions

From novice to tech pro — start learning today.