PHP: First name and Initial of Surname

What?
A note for myself on some code to convert a string of two names into a string made up of the first name and then using the initial of the second name.
copyraw
-- What I have
John Smith
Fred.Bloggs

-- What I want
John S.
Fred B.
  1.  -- What I have 
  2.  John Smith 
  3.  Fred.Bloggs 
  4.   
  5.  -- What I want 
  6.  John S. 
  7.  Fred B. 

How?
So different ways, the first thing I did was to create the logic:
copyraw
// default
$author_name_disp=$author_name;

// check and transform
if(strpos($author_name, ' ')!==false){
        $author_names=explode(' ', $author_name);
        $author_name_disp=ucfirst($author_names[0]).' '.strtoupper($author_names[1][0]).'.';
}elseif(strpos($author_name, '.')!==false){
        $author_names=explode('.', $author_name);
        $author_name_disp=ucfirst($author_names[0]).' '.strtoupper($author_names[1][0]).'.';
}else{
        $author_name_disp=ucfirst($author_name);
}

// output
echo $author_name_disp;
  1.  // default 
  2.  $author_name_disp=$author_name
  3.   
  4.  // check and transform 
  5.  if(strpos($author_name, ' ')!==false){ 
  6.          $author_names=explode(' ', $author_name)
  7.          $author_name_disp=ucfirst($author_names[0]).' '.strtoupper($author_names[1][0]).'.'
  8.  }elseif(strpos($author_name, '.')!==false){ 
  9.          $author_names=explode('.', $author_name)
  10.          $author_name_disp=ucfirst($author_names[0]).' '.strtoupper($author_names[1][0]).'.'
  11.  }else{ 
  12.          $author_name_disp=ucfirst($author_name)
  13.  } 
  14.   
  15.  // output 
  16.  echo $author_name_disp

A lot of repetition so lets reduce that a touch:
copyraw
// default
$author_name_disp=$author_name;

// check and transform
$delimiters=array(' ', '.');
foreach($delimiters as $word_index=>$delimiter) {
        if (strpos($author_name, $delimiter)!==false) {
                $author_names=explode($delimiters[$word_index], $author_name);
                $author_name_disp=ucfirst($author_names[0]).' '.strtoupper($author_names[1][0]).'.';
        }
}

// output
echo $author_name_disp;
  1.  // default 
  2.  $author_name_disp=$author_name
  3.   
  4.  // check and transform 
  5.  $delimiters=array(' ', '.')
  6.  foreach($delimiters as $word_index=>$delimiter) { 
  7.          if (strpos($author_name, $delimiter)!==false) { 
  8.                  $author_names=explode($delimiters[$word_index], $author_name)
  9.                  $author_name_disp=ucfirst($author_names[0]).' '.strtoupper($author_names[1][0]).'.'
  10.          } 
  11.  } 
  12.   
  13.  // output 
  14.  echo $author_name_disp
And a little more:
copyraw
// default
$author_name_disp=$author_name;

// check and transform
foreach(array(' ', '.') as $delimiter) {
        if (strpos($author_name, $delimiter)!==false) {
                $author_names=array_map('ucfirst', explode($delimiter, $author_name));
                $author_name_disp=$author_names[0].' '.$author_names[1][0].'.';
        }
}

// output
echo $author_name_disp;
  1.  // default 
  2.  $author_name_disp=$author_name
  3.   
  4.  // check and transform 
  5.  foreach(array(' ', '.') as $delimiter) { 
  6.          if (strpos($author_name, $delimiter)!==false) { 
  7.                  $author_names=array_map('ucfirst', explode($delimiter, $author_name))
  8.                  $author_name_disp=$author_names[0].' '.$author_names[1][0].'.'
  9.          } 
  10.  } 
  11.   
  12.  // output 
  13.  echo $author_name_disp
Category: Personal Home Page :: Article: 556

Credit where Credit is Due:


Feel free to copy, redistribute and share this information. All that we ask is that you attribute credit and possibly even a link back to this website as it really helps in our search engine rankings.

Disclaimer: Please note that the information provided on this website is intended for informational purposes only and does not represent a warranty. The opinions expressed are those of the author only. We recommend testing any solutions in a development environment before implementing them in production. The articles are based on our good faith efforts and were current at the time of writing, reflecting our practical experience in a commercial setting.

Thank you for visiting and, as always, we hope this website was of some use to you!

Kind Regards,

Joel Lipman
www.joellipman.com

Related Articles

Joes Revolver Map

Joes Word Cloud

create   different   elseif   john   logic   initial   note   index   myself   transform   smith   delimiter   repetition   string   word   else   made   reduce   strpos   author   ways   default   disp   foreach   using   array   convert   lets   names   delimiters   name   little   surname   check   touch   first   false   code   output   fred   bloggs   echo   second   thing   JoelLipman.Com

Accreditation

Badge - Certified Zoho Creator Associate
Badge - Certified Zoho Creator Associate

Donate & Support

If you like my content, and would like to support this sharing site, feel free to donate using a method below:

Paypal:
Donate to Joel Lipman via PayPal

Bitcoin:
Donate to Joel Lipman with Bitcoin bc1qf6elrdxc968h0k673l2djc9wrpazhqtxw8qqp4

Ethereum:
Donate to Joel Lipman with Ethereum 0xb038962F3809b425D661EF5D22294Cf45E02FebF
© 2024 Joel Lipman .com. All Rights Reserved.