#1
30.04.2020 um 07:22 UhrSehe gerade, dass ich "slashes" in den Methoden-Kommentaren geschrieben habe.
Ich meine natürlich "minus" bzw. "-" ...
Ich meine natürlich "minus" bzw. "-" ...
PHP :: Aufgabe #257 :: Lösung #1
<?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;<?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));
}
}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"
Kommentare:
Gelöschte Person
Punkte: 0
7 Kommentare