How to convert a piece of HTML code to plain text without leading and ending spaces in PHP?
如何在HTML中将一段HTML代码转换为纯文本而没有开头和结尾的空格?
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):
您可以在PHP中使用以下代码来去除HTML标记,删除开头和结尾的空格以及将特殊字符转换为HTML实体(如果将其放入HTML页面):
$linetxt = htmlspecialchars(trim(strip_tags($line)));
An example:
一个例子:
$ php -a
Interactiveshell
php > echo htmlspecialchars(trim(strip_tags('<div> <b>hello</b> world</div>'))) . PHP_EOL;
hello world
php >
Answered by Eric Z Ma.
埃里克·马(Eric Z Ma)回答。
翻译自: https://www.systutorials.com/how-to-convert-a-piece-of-html-code-to-plain-text-in-php/