composer require bicpi/html-converter-bundle
config/bundles.php:
return [
// ...
Bicpi\HtmlConverterBundle\BicpiHtmlConverterBundle::class => ['all' => true],
];
use Bicpi\HtmlConverterBundle\Converter\HtmlConverter;
class MyController extends AbstractController
{
public function convertAction(HtmlConverter $converter)
{
$html = '<h1>Hello</h1><p>World!</p>';
$text = $converter->convert($html);
// Returns: "Hello\n\nWorld!"
return new Response($text);
}
}
config/packages/bicpi_html_converter.yaml (if present).Resources/doc/index.md (for advanced usage).Service Injection Use dependency injection for reusable conversions:
public function __construct(private HtmlConverter $converter) {}
public function processEmail(string $htmlBody): string
{
return $this->converter->convert($htmlBody);
}
Twig Integration Add a Twig extension for frontend conversions:
// src/Twig/AppExtension.php
class AppExtension extends \Twig\Extension\AbstractExtension
{
public function __construct(private HtmlConverter $converter) {}
public function getFunctions(): array
{
return [
new \Twig\TwigFunction('html_to_text', [$this, 'convertHtml']),
];
}
public function convertHtml(string $html): string
{
return $this->converter->convert($html);
}
}
Batch Processing Loop through HTML strings (e.g., emails or CMS content):
$converter = $container->get(HtmlConverter::class);
$texts = array_map([$converter, 'convert'], $htmlArray);
Deprecated Package
Configuration Overrides
services.yaml overrides).src/DependencyInjection/BicpiHtmlConverterExtension.php for options.Edge Cases
DOMDocument for critical cases.????.
Tip: Test with your target locale early.$converter->convert($html, ['debug' => true]); // If supported
try {
return $converter->convert($html);
} catch (\Exception $e) {
return fallbackToPlainText($html);
}
Custom Rules
Extend the underlying HtmlConverter library (if source is available) to add:
*text*).<code> blocks).Event Listeners
Hook into Symfony events (e.g., kernel.response) to auto-convert HTML in responses:
// src/EventListener/HtmlConverterListener.php
class HtmlConverterListener
{
public function __construct(private HtmlConverter $converter) {}
public function onKernelResponse(GetResponseEvent $event)
{
$response = $event->getResponse();
if ($response->headers->contains('Content-Type', 'text/html')) {
$body = $this->converter->convert($response->getContent());
$response->setContent($body);
}
}
}
Alternative Libraries If the bundle is limiting, consider:
symfony/panther for browser-based conversion.paragonie/halite for cryptographic text extraction.How can I help you explore Laravel packages today?