How to Parse, Validate Full Names in PHP

Cloudmersive
1 min readApr 27, 2023

--

The below code will allow you to easily parse full name strings (e.g., Mr. John Smith Jr.) into an array of data containing each component of the input name. For example, the above name would return the following information:

{
"Successful": true,
"ValidationResult_FirstName": "ValidFirstName",
"ValidationResult_LastName": "ValidLastName",
"Title": "Mr.",
"FirstName": "John",
"MiddleName": null,
"LastName": "Smith",
"NickName": null,
"Suffix": "Jr.",
"DisplayName": "John Smith"
}

The response body will validate the first and last name and return titles, middle names, nick names and suffixes (if available) in an organized fashion.

To take advantage of this API, you can begin by running the below command to install the client SDK:

composer require cloudmersive/cloudmersive_validate_api_client

After that, you can copy & paste from the below code examples to structure your API call:

<?php
require_once(__DIR__ . '/vendor/autoload.php');

// Configure API key authorization: Apikey
$config = Swagger\Client\Configuration::getDefaultConfiguration()->setApiKey('Apikey', 'YOUR_API_KEY');



$apiInstance = new Swagger\Client\Api\NameApi(


new GuzzleHttp\Client(),
$config
);
$input = new \Swagger\Client\Model\FullNameValidationRequest(); // \Swagger\Client\Model\FullNameValidationRequest | Validation request information

try {
$result = $apiInstance->nameValidateFullName($input);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling NameApi->nameValidateFullName: ', $e->getMessage(), PHP_EOL;
}
?>

Now simply configure your name string input and provide a free-tier API key (get one by visiting the Cloudmersive website), and you’re good to go. Easy!

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

No responses yet