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: The information on this website is provided without warranty and any content is merely the opinion of the author. Please try to test in development environments prior to adapting them to your production environments. The articles are written in good faith and, at the time of print, are working examples used 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

touch   initial   reduce   else   create   string   john   default   output   check   code   echo   thing   second   name   author   first   myself   delimiter   convert   using   made   repetition   delimiters   array   ways   little   logic   strpos   elseif   transform   lets   fred   word   different   foreach   smith   names   disp   false   index   surname   bloggs   note   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 - Valid till 8 May 2022 3QnhmaBX7LQSRsC9hh6Je9rGQKEGNQNfPb
© 2021 Joel Lipman .com. All Rights Reserved.