[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Getting someones initials from a string of their name in PHP

Posted on 2004-11-08
8
Medium Priority
?
793 Views
Last Modified: 2008-03-10
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
0
Comment
Question by:rfila
8 Comments
 
LVL 49

Expert Comment

by:Roonaan
ID: 12523333
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
 
LVL 3

Expert Comment

by:nesnemis
ID: 12523339
Hi rfila,
try this:

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

nesnemis
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 12523655
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 6

Expert Comment

by:mattjp88
ID: 12525276
<?
$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
 
LVL 6

Expert Comment

by:mattjp88
ID: 12525290
forgot something.  change:

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

to:

$nameTemp=substr(trim($nameArray[$i]),0,1);
0
 
LVL 2

Expert Comment

by:suresh_asp
ID: 12527391
try this regular expression,

preg_match_all("/\b\S/","Benjamin James Smith",$out);
foreach($out as $value){
        echo implode("",$value);
}
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 12527420
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
 
LVL 10

Accepted Solution

by:
eeBlueShadow earned 500 total points
ID: 12529176
Here's a very neat solution:

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


_Blue
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month20 days, 8 hours left to enroll

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question