How to convert a piece of HTML code to plain text in PHP?

How to convert a piece of HTML code to plain text without leading and ending spaces in PHP?

For example, I would like to convert

<div> <b>hello</b> world</div>

to a string

hello world
asked Aug 12 by anonymous

1 Answer

You may use this piece of code in PHP to strip HTML tags, remove leading and ending spaces and convert special characters to HTML entities (if you put this into an HTML page):

$linetxt = htmlspecialchars(trim(strip_tags($line)));

An example:

$ php -a
Interactive shell

php > echo htmlspecialchars(trim(strip_tags('<div> <b>hello</b> world</div>'))) . PHP_EOL;
hello world
php > 
answered Aug 12 by Eric Z Ma (43,500 points)

Please log in or register to answer this question.

Related questions

Copyright © SysTutorials. User contributions licensed under cc-wiki with attribution required.
Hosted on Dreamhost

...