How to convert a piece of HTML code to plain text in PHP?
Posted on In QAHow 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
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 >
Thank you so much ^_^