How to trim leading and trailing Whitespace from a Text String in PHP
The presence of extra whitespace characters in a string can certainly lead to its share of problems. With that in mind, today’s guide covers the process of trimming leading and trailing whitespace from any string of text. While not too bad on its own, we are going to make the solution to this even simpler by applying a Cloudmersive API. That way, we’ll all be home for supper.
First, our installation command:
composer require cloudmersive/cloudmersive_document_convert_api_client
Second, our function:
<?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\EditTextApi(// If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.// This is optional, `GuzzleHttp\Client` will be used as default.new GuzzleHttp\Client(),$config);$request = new \Swagger\Client\Model\RemoveWhitespaceFromTextRequest(); // \Swagger\Client\Model\RemoveWhitespaceFromTextRequest | Input requesttry {$result = $apiInstance->editTextTrimWhitespace($request);print_r($result);} catch (Exception $e) {echo 'Exception when calling EditTextApi->editTextTrimWhitespace: ', $e->getMessage(), PHP_EOL;}?>
Third… all done! That’s right, consider your whitespace problems a thing of the past. Just drop your strings into editTextTrimWhitespace, and you are good to go.