How to Convert HTML to Text using PHP

Cloudmersive
1 min readFeb 16, 2023

--

Plain text content lives within the body of any HTML code snippet — and separating that text from its HTML casing takes more time and effort that we usually have. Thankfully, the below API performs this conversion instantaneously, removing all HTML formatting from text and returning that text as a plain string. It’s easy to use — simply follow instructions below to implement & structure your request and use a free-tier API key (get one by visiting our website) to make up to 800 API calls per month with zero commitments.

First things first, run this command to install the SDK:

composer require cloudmersive/cloudmersive_document_convert_api_client

After that, call the function, and copy your API key into the $config field:

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


new GuzzleHttp\Client(),
$config
);
$input = new \Swagger\Client\Model\HtmlToTextRequest(); // \Swagger\Client\Model\HtmlToTextRequest | HTML to Text request parameters

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

Make sure to include your HTML input as a string, and you’re good to go. Easy as that!

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet