nameFix()
Standardises names to conform to specific naming conventions with proper capitalisation.
Table of contents
Signature
public static function nameFix(#[\SensitiveParameter] ?string $lastName): ?string
Parameters
| Parameter | Type | Description |
|---|---|---|
$lastName |
?string |
The name to standardise. Marked as sensitive parameter. |
Returns
?string - The standardised name, or null if input was null.
Description
The nameFix() method standardises names by applying consistent capitalisation rules and handling various naming conventions correctly. It performs:
- UTF-8 to ANSI conversion - For consistent character handling
- Accent removal - Normalises characters
- Mc/Mac prefix handling - Adds proper spacing and capitalisation
- Hyphenated name handling - Capitalises each part
- Common prefix correction - Handles van, von, de, du, la, le, etc.
- Space normalisation - Reduces multiple spaces
Performance
- ~130,000 operations per second
- Consolidated regex operations
- Optimised prefix handling
Security
The parameter is marked with #[\SensitiveParameter] to prevent name data from appearing in stack traces and error logs.
Examples
Basic Usage
use MarjovanLier\StringManipulation\StringManipulation;
$result = StringManipulation::nameFix('mcdonald');
echo $result; // Output: McDonald
Scottish/Irish Names
// Mc prefix
StringManipulation::nameFix('mcdonald');
// Output: McDonald
// Mac prefix
StringManipulation::nameFix('macdonald');
// Output: MacDonald
// O' prefix
StringManipulation::nameFix("o'brien");
// Output: O'Brien
Dutch/German Prefixes
// van
StringManipulation::nameFix('van der waals');
// Output: van der Waals
// von
StringManipulation::nameFix('von neumann');
// Output: von Neumann
// de
StringManipulation::nameFix('de souza');
// Output: de Souza
Hyphenated Names
StringManipulation::nameFix('smith-jones');
// Output: Smith-Jones
StringManipulation::nameFix("o'brien-smith");
// Output: O'Brien-Smith
Null Handling
$result = StringManipulation::nameFix(null);
// Result: null
Prefix Handling
The method handles these common prefixes by keeping them lowercase:
| Prefix | Example Input | Output |
|---|---|---|
| van | van berg |
van Berg |
| von | von stein |
von Stein |
| de | de silva |
de Silva |
| du | du pont |
du Pont |
| la | la rue |
la Rue |
| le | le blanc |
le Blanc |
| der | van der berg |
van der Berg |
| den | van den berg |
van den Berg |
Use Cases
User Registration
class UserRegistration
{
public function normaliseUserName(string $lastName): string
{
$normalised = StringManipulation::nameFix($lastName);
if ($normalised === null) {
throw new InvalidArgumentException('Last name is required');
}
return $normalised;
}
}
$registration = new UserRegistration();
$name = $registration->normaliseUserName('VAN DER BERG');
// Result: van der Berg
Data Import Standardisation
function standardiseNames(array $users): array
{
return array_map(function ($user) {
return [
...$user,
'last_name' => StringManipulation::nameFix($user['last_name']),
];
}, $users);
}
Display Formatting
function formatDisplayName(string $firstName, string $lastName): string
{
$standardisedLast = StringManipulation::nameFix($lastName);
return sprintf('%s %s', ucfirst($firstName), $standardisedLast);
}
echo formatDisplayName('john', 'mcdonald');
// Output: John McDonald
Related Methods
searchWords()- Includes name fixing with search optimisationremoveAccents()- Accent removal onlyutf8Ansi()- Encoding conversion