How to Convert HTML to Plain Text in PHP


The Hypertext PHP package converts HTML to pure text and beautifully handles various and malformed HTML. It works by taking an HTML string as input and removing all the markup, leaving the plain text:

My Blog

Welcome to My Blog
This is a paragraph of text on my webpage.
Click here to view my posts.


Using Hypertext's Transformer class, you can convert the HTML to pure text like so:
use Stevebauman\Hypertext\Transformer;

// Pure text output
echo (new Transformer)->toText($input);

// Welcome to My Blog This is a paragraph of text on my webpage. Click here to view my posts.

Let's say that you want to convert everything to text but also preserve newlines and links:
echo (new Transformer)
->keepLinks()
->keepNewLines()
->toText($input);

/*
Welcome to My Blog
This is a paragraph of text on my webpage.
Click Here to view my posts.
*/

Here is a list of the top features in the hypertext package:

  • Removes CSS
  • Removes scripts
  • Removes headers
  • Removes non-HTML based content
  • Preserves spacing
  • Preserves links (optional)
  • Preserves new lines (optional)

You can find the complete details for this package on Github: stevebauman/hypertext.

The post How to Convert HTML to Plain Text in PHP appeared first on Laravel News.
Join the Laravel Newsletter to get Laravel articles like this directly in your inbox.