Getting someones initials from a string of their name in PHP

Hi

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.

Thanks
Rich
rfilaAsked:
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.

RoonaanCommented:
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.

Regards

-r-
0
nesnemisCommented:
Hi rfila,
try this:

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

nesnemis
0
RoonaanCommented:
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:

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

Regards

-r-
0
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.

mattjp88Commented:
<?
$name="Experts Exchange";
$nameArray=explode(" ",$name);
$initials="";
for ($i=0;$i<sizeof($nameArray);$i++) {
$nameTemp=substring($nameArray[$i],0,1);
$initials.=$nameTemp;
}

$initials=strtoupper($initials);

echo "Initials: $initials";
?>
0
mattjp88Commented:
forgot something.  change:

$nameTemp=substring($nameArray[$i],0,1);

to:

$nameTemp=substr(trim($nameArray[$i]),0,1);
0
suresh_aspCommented:
try this regular expression,

preg_match_all("/\b\S/","Benjamin James Smith",$out);
foreach($out as $value){
        echo implode("",$value);
}
0
RoonaanCommented:
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...

-r-
0
eeBlueShadowCommented:
Here's a very neat solution:

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


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

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.