How to Validate Code Identifiers in PHP
Validating code identifiers is a key step towards ensuring consistent, syntactically correct code.
Using the free API provided in this article, you can quickly and easily validate strings of code with custom rules (such as determining whether whitespace, hyphens, or periods are allowed). Here’s an example of how a full JSON input request can be structured:
{
"Input": "string",
"AllowWhitespace": true,
"AllowHyphens": true,
"AllowUnderscore": true,
"AllowNumbers": true,
"AllowPeriods": true,
"MaxLength": 0,
"MinLength": 0
}
The API response will determine if the identifier is valid and return an error string if any problems are found.
To take advantage of this API with ready-to-run PHP Code examples, first run the following command to install the client SDK:
composer require cloudmersive/cloudmersive_validate_api_client
After that, copy the below code 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\ValidateIdentifierRequest(); // \Swagger\Client\Model\ValidateIdentifierRequest | Identifier validation request information
try {
$result = $apiInstance->nameIdentifier($input);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling NameApi->nameIdentifier: ', $e->getMessage(), PHP_EOL;
}
?>
To authenticate your requests, supply a free-tier API key (you can get one by registering a free account on the Cloudmersive website). Then you’re all done!