【问题标题】:plaintext in php simple html parse dom with "a" and "img" tag带有“a”和“img”标签的php简单html解析dom中的纯文本
【发布时间】:2015-02-26 01:47:12
【问题描述】:

我有简单的富文本,例如:

<div><p> some text <br/> some text <br/> 
    <img src=pic.jpeg> and  <a href="web.html">link</a> </p>
</div>

在这种情况下我是否有可能使用简单的 html dom 获得纯文本:

   some text some text <img src=pic.jpeg> and <a href="web.html">link</a>

我的意思是每个标签都将被删除,除了一个标签和 img 标签

【问题讨论】:

  • simplehtmldom.sourceforge.net 研究这个网站。希望你能自己得到答案。
  • 在其文档中,我认为这是不可能的!但是我可以通过一个技巧来解决它
  • 第一次尝试。如果您失败,请在此处回复。我会为你解决的。

标签: php html html-parsing simple-html-dom


【解决方案1】:

除非我误解了问题,否则您只需 strip_tags() 即可:

$str = '<div><p> some text <br/> some text <br/> 
          <img src=pic.jpeg> and  <a href="web.html">link</a> </p>
        </div>';

echo htmlspecialchars(strip_tags($str, '<a><img>'));

// result
some text some text <img src=pic.jpeg> and <a href="web.html">link</a> 

an example

请注意,我只使用了htmlspecialchars 在浏览器中显示结果。

【讨论】:

    猜你喜欢
    • 2013-08-15
    • 1970-01-01
    • 1970-01-01
    • 2013-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-30
    • 2018-03-23
    相关资源
    最近更新 更多