【问题标题】:PHP strip_tags: allow just first tagPHP strip_tags:只允许第一个标签
【发布时间】:2016-11-16 01:46:19
【问题描述】:

我有这个代码strip_tags($html,'<a>,<br>'),它会去除除<a><br> 之外的所有标签

如何去除除第一个之外的所有<a> 标签?有strip_tags($html,'<a>[0],<br>')之类的吗?

HTML 示例:

<div>
    <p>
        <a href="#">I want this link</a><br/>
        Something in new line with unwantend <a href="#">link</a><br/>
        Lorem ipsum bla bla...<br/>
        Unwanted <a href="#">link</a> once more.
    </p>
</div>

【问题讨论】:

  • 你将不得不编写一些代码来做到这一点,也许是正则表达式
  • @Dagon 好的,谢谢。我想也许有更短的方法。
  • @SpacePeasant 如果我们知道他的上下文可能会有
  • @Machavity we all want a pony

标签: php


【解决方案1】:

根据原始答案编辑:

你可以使用 preg_replace 和一个循环

 $ret = preg_match_all('/<a>/',$html,$matches);
   for ($a = count($matches); $a>=1 ;$a--){
      $str = preg_replace('/<a>/',"",$str,$a);
   }

【讨论】:

  • 这不是 OP 所要求的
  • 一切都好... :-) 请注意通过正则表达式弄乱 html 是多么危险
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-15
相关资源
最近更新 更多