bicpi/html-converter
PHP library for converting HTML to plain text, ideal for generating text parts of HTML emails. Includes Simple (strip_tags), Lynx (best results, keeps links), Html2Text, and Chain converter to pick the first available backend.
Installation:
composer require bicpi/html-converter
No additional configuration is required—just autoload the package.
First Use Case: Convert a simple HTML string to plain text:
use Bicpi\HtmlConverter\HtmlConverter;
$html = '<h1>Hello World</h1><p>This is <strong>HTML</strong>.</p>';
$converter = new HtmlConverter();
$text = $converter->convert($html);
// Output: "Hello World\n\nThis is HTML."
Where to Look First:
src/HtmlConverter.php for core logic and available methods.Basic Conversion:
$converter = new HtmlConverter();
$text = $converter->convert($htmlString);
<h1>–<h6>, <p>, <strong>, <em>, <a>, etc.).<h1> → newline, <p> → double newline).Customizing Output:
<a> tags:
$converter->setLinkHandler(function ($url, $text) {
return "[{$text}]({$url})";
});
$converter->setAllowedTags(['h1', 'h2', 'p', 'ul', 'li']);
Integration with Laravel:
// app/Providers/AppServiceProvider.php
public function register()
{
$this->app->singleton(HtmlConverter::class);
}
use Bicpi\HtmlConverter\HtmlConverter;
public function showTextVersion(Request $request)
{
$html = $request->input('html_content');
$text = app(HtmlConverter::class)->convert($html);
return response()->json(['text' => $text]);
}
Batch Processing:
$htmlStrings = [/* array of HTML strings */];
$converter = new HtmlConverter();
$texts = array_map([$converter, 'convert'], $htmlStrings);
Edge Cases:
<strong><em>text</em></strong> → text (bold + italic).<script> and <style> by default (configurable via setAllowedTags).Line Break Sensitivity:
\n for newlines. If your system expects <br> or <br/> to be preserved, manually replace them:
$text = str_replace(['<br>', '<br/>'], "\n", $html) ?? $html;
$text = $converter->convert($text);
handleBrTag method in a subclass if needed.URL Handling:
<a>) default to [text](url). If you need raw URLs:
$converter->setLinkHandler(function ($url) {
return $url;
});
<a href="javascript:alert()">) may break. Sanitize input first:
$html = filter_var($html, FILTER_SANITIZE_URL, FILTER_FLAG_NO_ENCODE);
Performance:
HtmlConverter per request in high-traffic apps. Use Laravel’s service container (see Implementation Patterns).Whitelisting Quirks:
setAllowedTags([]) strips all HTML. Use cautiously.class CustomConverter extends HtmlConverter {
public function __construct() {
$this->setAllowedTags(['div', 'span']); // Example
}
}
Inspect Intermediate Steps:
$converter->setDebug(true);
$text = $converter->convert($html);
// Check $converter->getDebugOutput() for details.
Test Edge Cases:
<table>, <tr>, <td> are stripped by default. Add them to setAllowedTags() if needed.data-*) are ignored. Use setAttributeHandler to customize:
$converter->setAttributeHandler(function ($tag, $attributes) {
return array_filter($attributes, fn($k) => str_starts_with($k, 'data-'), ARRAY_FILTER_USE_KEY);
});
Character Encoding:
mb_convert_encoding() if needed:
$html = mb_convert_encoding($html, 'UTF-8', 'auto');
Custom Tag Handlers:
handleTag() to process tags uniquely:
$converter->setTagHandler('code', function ($text) {
return '`' . $text . '`';
});
Pre/Post-Processing:
Str::of() or Str::limit() for truncation:
use Illuminate\Support\Str;
$text = Str::of($converter->convert($html))->limit(200);
Output Formatting:
setTextHandler() to modify the final text:
$converter->setTextHandler(function ($text) {
return preg_replace('/\n{3,}/', "\n\n", $text); // Collapse excessive newlines
});
How can I help you explore Laravel packages today?