How to validate a Code Identifier in PHP

I’m going to go out on a limb here and guess that you aren’t exactly thrilled at the prospect of learning how to validate code identifiers. And really, who needs all that hassle and headache? How about I show you a different way to do it? A way that requires almost no effort whatsoever. An API is about to rescue us from our boring fate.

Let’s start things up by installing our API client with this command here:

composer require cloudmersive/cloudmersive_validate_api_client

Now we can call this function and configure our input rules concerning whitespace, hyphens, etc.

<?phprequire_once(__DIR__ . '/vendor/autoload.php');// Configure API key authorization: Apikey$config = Swagger\Client\Configuration::getDefaultConfiguration()->setApiKey('Apikey', 'YOUR_API_KEY');// Uncomment below to setup prefix (e.g. Bearer) for API key, if needed// $config = Swagger\Client\Configuration::getDefaultConfiguration()->setApiKeyPrefix('Apikey', 'Bearer');$apiInstance = new Swagger\Client\Api\NameApi(// If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.// This is optional, `GuzzleHttp\Client` will be used as GuzzleHttp\Client(),$config);$input = new \Swagger\Client\Model\ValidateIdentifierRequest(); // \Swagger\Client\Model\ValidateIdentifierRequest | Identifier validation request informationtry {$result = $apiInstance->nameIdentifier($input);print_r($result);} catch (Exception $e) {echo 'Exception when calling NameApi->nameIdentifier: ', $e->getMessage(), PHP_EOL;}?>

Now enter a code identifier to test it out, because it’s already working! It’s as simple as that.

Written by

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

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store