forked from jasonpriem/HumanNameParser.php
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathparse_name.php
More file actions
66 lines (56 loc) · 2.05 KB
/
parse_name.php
File metadata and controls
66 lines (56 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
require_once __DIR__ . '/../vendor/autoload.php';
use HumanNameParser\Parser;
// Check if a name was provided as a command-line argument
if ($argc > 1) {
// Get the name from the command-line arguments
$nameString = implode(' ', array_slice($argv, 1));
} else {
// Example names to parse
$examples = [
"John Smith",
"Dr. Jane Smith",
"Smith, John",
"John William Smith",
"John Smith Jr.",
"Dr. John W. ('Johnny') Smith-Brown, III",
"Vincent van Gogh",
"James C. O'Neill",
"Björn O'Malley, Jr."
];
// Let user choose an example or enter their own name
echo "Choose an example to parse:\n";
foreach ($examples as $i => $example) {
echo ($i + 1) . ". $example\n";
}
echo ($i + 2) . ". Enter your own name\n";
echo "Your choice (1-" . ($i + 2) . "): ";
$choice = (int) trim(fgets(STDIN));
if ($choice === count($examples) + 1) {
echo "Enter a name to parse: ";
$nameString = trim(fgets(STDIN));
} elseif ($choice > 0 && $choice <= count($examples)) {
$nameString = $examples[$choice - 1];
} else {
echo "Invalid choice. Using first example.\n";
$nameString = $examples[0];
}
}
// Create a new Parser instance
$parser = new Parser();
try {
// Parse the name
$name = $parser->parse($nameString);
// Display the results
echo "\nParsing results for: " . $nameString . "\n";
echo str_repeat("-", 40) . "\n";
echo "Academic Title: " . ($name->getAcademicTitle() ?? "N/A") . "\n";
echo "Leading Initial: " . ($name->getLeadingInitial() ?? "N/A") . "\n";
echo "First Name: " . ($name->getFirstName() ?? "N/A") . "\n";
echo "Nicknames: " . ($name->getNicknames() ?? "N/A") . "\n";
echo "Middle Name: " . ($name->getMiddleName() ?? "N/A") . "\n";
echo "Last Name: " . ($name->getLastName() ?? "N/A") . "\n";
echo "Suffix: " . ($name->getSuffix() ?? "N/A") . "\n";
} catch (Exception $e) {
echo "Error parsing name: " . $e->getMessage() . "\n";
}