Getting someones initials from a string of their name in PHP


I have a string of a name eg "Benjamin James Smith".

I need to get the initials of that name into another string eg "BJS".

At present I am having to split the string by spaces and substr each element of the array.  I'm sure their must be a simple way with ereg_replace but I dont have a good enough understanding of the characters you put into that function to make it do what you want!

Can someone tell me how to perform the above function and also point me in the right direction for documentation of how that function works.

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.

Try the next line:

echo ereg_replace('[^A-Z]','', ucwords(strtolower($name)));

There will be an preg variant offcourse, but this is the ereg version.


Hi rfila,
try this:

$nameArray = split(" ", $usersFullName);
$initials = "";
for($i=0; $i<$nameArray.length(); $i++)
    $initials .= nameArray[$i][0];
echo $initials;

Re: nesnemis:

>At present I am having to split the string by spaces and substr each element of the array.

Therefor your solution isn't quite a solution in this case.

Furthermore $nameArray.length() is nonexisting in php - to my knowledge at least.

Evenso, if you would use the php equivalent of length() (eq: count($nameArray)) you're code would still be inefficient and should be something like:

$nameArray = split(" ", $usersFullName);
foreach($nameArray as $name)
  echo substr(trim($name),0,1);


Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

$name="Experts Exchange";
$nameArray=explode(" ",$name);
for ($i=0;$i<sizeof($nameArray);$i++) {


echo "Initials: $initials";
forgot something.  change:



try this regular expression,

preg_match_all("/\b\S/","Benjamin James Smith",$out);
foreach($out as $value){
        echo implode("",$value);
pfff. the better use $text = ereg_replace("[A-Z]",'',$text);

But that solution will not prevent issues with usernames which aren't entered with capitals.

Therefor you should use ucwords(strtolower($text)) to get all and only the first letters of each word in uppercase.

Also mattjp88's is nice, but is what rfila already has, and asked us to improve...

Here's a very neat solution:

    $str = "Experts exchange Name";
    echo preg_replace('/(\B\w)|\s/', '', $str);


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
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

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.