How to extract only text from HTML string with PHP?

后端 未结 3 1559
野的像风
野的像风 2020-12-09 19:52

I want to extract only text from a php string.

This php string contains html code like tags or etc.

So I only need a simple text from this string.

<
3条回答
  •  隐瞒了意图╮
    2020-12-09 20:22

    Another option is to use Html2Text. It will do a much better job than strip_tags, especially if you want to parse complicated HTML code.

    Extracting text from HTML is tricky, so your best bet is to use a library built for this purpose.

    https://github.com/mtibben/html2text

    Install using composer:

    composer require html2text/html2text
    

    Basic usage:

    $html = new \Html2Text\Html2Text('Hello, "world"');
    
    echo $html->getText();  // Hello, "WORLD"
    

提交回复
热议问题