PHP :: Aufgabe #257

1 Lösung Lösung öffentlich

Namen in Initialen umwandeln

Anfänger - PHP von JKooP - 29.03.2020 um 15:07 Uhr
Schreibe ein Programm, mit dem es möglich ist, einen beliebig langen Namen (Vorname(n), Nachname(n), Namenszusätze) als Initialen darzustellen.
Ebenfalls sollen durch Bindestrich getrennte Namen einbezogen werden.
Optional soll die Ausgabe sowohl mit Bindestrich als auch in Großbuchstaben erfolgen.

Beispiel:

Karl-Theodor Maria Nikolaus Johann Jacob Philipp Franz Joseph Sylvester Buhl-Freiherr von und zu Guttenberg

--> KTMNJJPFJSBFvuzG

Optional:

--> K-TMNJJPFJSB-FvuzG
--> KTMNJJPFJSBFVUZG oder K-TMNJJPFJSB-FVUZG

Viel Spaß

Lösungen:

1 Kommentar
vote_ok
von Exception (7090 Punkte) - 16.04.2020 um 19:56 Uhr
main.php
Quellcode ausblenden PHP-Code
<?php

require_once('InitialsGenerator.php');

array_shift($argv); // remove script name

if (sizeof($argv) !== 1) {
  die ('ERROR: 1 parameter needed, ' . sizeof($argv) . ' provided.' . PHP_EOL);
}

$name = $argv[0];
$initials1 = InitialsGenerator::getInitials_1($name);
$initials2 = InitialsGenerator::getInitials_2($name);
$initials3a = InitialsGenerator::getInitials_3a($name);
$initials3b = InitialsGenerator::getInitials_3b($name);

echo 'NAME        : "' , $name , '"' , PHP_EOL;
echo 'INITIALS 1  : "' , $initials1 , '"' , PHP_EOL;
echo 'INITIALS 2  : "' , $initials2 , '"' , PHP_EOL;
echo 'INITIALS 3a : "' , $initials3a , '"' , PHP_EOL;
echo 'INITIALS 3b : "' , $initials3b , '"' , PHP_EOL;


InitialsGenerator.php
Quellcode ausblenden PHP-Code
<?php

final class InitialsGenerator
{
  private function __construct() { }

  private static function getMatches(string $pattern, string $subject): array
  {
    preg_match_all($pattern, $subject, $matches);
    return $matches[0];
  }

  /**
  * Get the initials with upper- and lowercase characters mixed.
  * Also without slashes.
  */
  public static function getInitials_1(string $name): string 
  {
    return implode('', self::getMatches('/\b[A-Za-z]/', $name));
  } 

  /**
  * Get the initials with upper- and lowercase characters mixed.
  * But this time with slashes.
  */
  public static function getInitials_2(string $name): string 
  {
    return implode('', self::getMatches('/\b[A-Za-z\-]/', $name));
  } 

  /**
  * Get the initials with only uppercase characters.
  * Also without slashes.
  */
  public static function getInitials_3a(string $name): string 
  {
    return strtoupper(self::getInitials_1($name));
  }

  /**
  * Get the initials with uppercase characters.
  * But this time with slashes.
  */
  public static function getInitials_3b(string $name): string 
  {
    return strtoupper(self::getInitials_2($name));
  }
}


script call with php main.php "Karl-Theodor Maria Nikolaus Johann Jacob Philipp Franz Joseph Sylvester Buhl-Freiherr von und zu Guttenberg" results in:

Konsolenausgabe:

NAME        : "Karl-Theodor Maria Nikolaus Johann Jacob Philipp Franz Joseph Sylvester Buhl-Freiherr von und zu Guttenberg"
INITIALS 1 : "KTMNJJPFJSBFvuzG"
INITIALS 2 : "K-TMNJJPFJSB-FvuzG"
INITIALS 3a : "KTMNJJPFJSBFVUZG"
INITIALS 3b : "K-TMNJJPFJSB-FVUZG"