Print

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