How to Remove HTML Markup from Text in PHP

Cloudmersive
2 min readJun 10, 2024

--

Removing HTML from text is an easy and effective way to sanitize client-side text inputs.

It’s even easier if we don’t have to write any of that sanitizing code ourselves. We can use the complementary PHP code examples below to call a free API that sanitizes text input for us, stripping HTML code away and returning nothing but plain text.

To structure our API call, we’ll just need to install the client SDK with Composer:

composer require cloudmersive/cloudmersive_document_convert_api_client

And then we can use the below examples to call the HTML removal function:

<?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\EditTextApi(


new GuzzleHttp\Client(),
$config
);
$request = new \Swagger\Client\Model\RemoveHtmlFromTextRequest(); // \Swagger\Client\Model\RemoveHtmlFromTextRequest | Input request

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

Note that we’ll need to authorize our API call with a free Cloudmersive API key. This will give us a limit of 800 API calls per month with zero commitments (our total will simply reset the following month once we reach it).

That’s all there is to it! We can easily remove HTML formatting from text with just a few lines of PHP.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

Responses (1)