Skip to content

Latest commit

 

History

28 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

HTML to Plain Text

A simple, lightweight, and effective "HTML to Plain Text" converter for your PHP projects 🪄

📄 Overview

Ko Fi - Offer Me A Coffee

Simple. Clean. Efficient. Just what you need to convert HTML code into plain text 🧹

🐘 PHP Requirement

  • PHP v7.4 or newer.
  • PHP DOM extension, used to extract full HTML document bodies.

🛠 Installation

composer require ph-7/html-to-text

If you don't already use composer in your project, include Composer's autoload as shown below in your main PHP index file of your project.

require __DIR__ . '/vendor/autoload.php';

🥳 Usage

use PH7\HtmlToText\Convert;

$htmlCode = '<div><p><em>Some random paragraphs...</em></p></div>';

$this->html2Text = new Convert($htmlCode);
$plainText = $this->html2Text->getText();

echo $plainText;

getText() returns plain text, which can contain literal < and > characters. Escape it with htmlspecialchars($plainText, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8') when embedding it in an HTML page. This converter is not an HTML sanitiser. Full-document parsing uses PHP's legacy DOMDocument HTML parser, so malformed or modern HTML can have parser-specific edge cases.

😋 Who cooked it?

Pierre-Henry Soria

@phenrysay @pH7Programming pH-7

Pierre-Henry Soria. A super passionate, zen & pragmatic software engineer 😊 Cheese & dark chocolate lover! 🧀🍫😋

️☕️ Are you enjoying it...? You could offer me a coffee if you wish 😋

Projects using it 🚀

⚖️ License

HTML to Text is generously distributed under MIT 🎉 Enjoy!