How to replace Text with a Regex Regular Expression String in PHP
2 min readApr 15, 2020
I’m not here to tell you why Regex regular expression strings are important — you probably already know. Instead, you are here so I can demonstrate how to expedite the implementation of search and replace for said Regex strings. It’s simple enough if you know the trick that I am about to share with you.
In Composer, open the command line and run the following command.
composer require cloudmersive/cloudmersive_document_convert_api_client
This will install our client, which will in turn open access to the API function that we need here:
<?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\ReplaceStringRegexRequest(); // \Swagger\Client\Model\ReplaceStringRegexRequest | Input requesttry {$result = $apiInstance->editTextReplaceRegex($request);print_r($result);} catch (Exception $e) {echo 'Exception when calling EditTextApi->editTextReplaceRegex: ', $e->getMessage(), PHP_EOL;}?>
Now you can use editTextReplaceRegex to do all the work for you. Easy as that.