5

嗨,请任何人为此给我一个解决方案。我想从这个 html 中删除整个 div 和 span,除了 4 in <span>. 请任何一个

<div class=\"fivestar-widget-static fivestar-widget-static-vote fivestar-widget-static-5 clear-block\">
  <div class=\"star star-1 star-odd star-first\">
    <span class=\"on\">4</span>
  </div>
  <div class=\"star star-2 star-even\">
    <span class=\"on\"></span>
  </div>
  <div class=\"star star-3 star-odd\">
    <span class=\"on\"></span>
  </div>
  <div class=\"star star-4 star-even\">
    <span class=\"on\"></span>
  </div>
  <div class=\"star star-5 star-odd star-last\">
    <span class=\"off\"></span>
  </div>
</div>
4

1 回答 1

10

要去除 HTML 标签但保留其文本内容,您可以使用strip_tags

$string = "<div>Hello</div> <span>Hi</span> Other text";
$string = strip_tags($string);

// You'll probably also want to trim the results 
// to remove extraneous whitespace
$string = trim($string);

这将导致"Hello Hi Other text"

PHP 手册: http: //php.net/manual/en/function.strip-tags.php

于 2013-07-13T11:19:21.307 回答