【问题标题】:Can't seem to get rid of unwanted tags returned from API似乎无法摆脱 API 返回的不需要的标签
【发布时间】:2013-03-06 01:32:17
【问题描述】:

我需要删除从 Petfinder API 返回的描述中的所有 HTML 标记,但我似乎不知道该怎么做。

$data['description'] 保存返回的描述。我在源文件中可以看到是这样的......

<div>some text that gets returned</div>

我尝试过使用strip_tags,我尝试过使用html_entity_decode,但标签不会消失!

我需要去除标签以便截断描述..

有人有什么想法吗?

$data['description'] = (string)$pet->description;

$description = $data['description'];
$description = htmlentities($description);
$description = html_entity_decode($description);
$description = strip_tags($description);

$description = substr($data['description'],0,300);
$description = substr($description,0,strrpos($description,' '));
$description = $description."...";

echo "<span style='text-align: justify; margin: 10px 0px;'>".$description."</span>";

【问题讨论】:

  • 显示描述文本..

标签: php html-entities strip-tags


【解决方案1】:

问题是您在处理过程中再次使用原始输入:

$description = $data['description'];
$description = htmlentities($description);
$description = html_entity_decode($description);
$description = strip_tags($description);

$description = substr($data['description'],0,300);
                      ^^^^^^^^^^^^^^^^^^^^ All previous changes undone!
$description = substr($description,0,strrpos($description,' '));
$description = $description."...";

除此之外,@redreggae 是对的,你只需要strip_tags

【讨论】:

  • 它总是最简单的事情......感谢你,感谢@redreggae!
【解决方案2】:

问题是你先运行htmlentities。 在这之后strip_tags 不能再工作了。

简单地做:

$test = '<div>some text that gets returned</div>';
echo strip_tags($test);

硬核方式:

$doc = new DOMDocument();
$doc->loadHTML('<div>some text that gets returned</div>');
$items = $doc->getElementsByTagName('div');
$content = $items->item(0)->nodeValue;
echo $content;

【讨论】:

  • 这不起作用。我从 strip_tags 开始,它什么也没做,所以我添加了 html_entity_decode 认为标签可能是 htmlentities。那没有用,所以我添加了 htmlentities,但它不起作用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-01-14
  • 2018-12-14
  • 2011-12-16
  • 1970-01-01
  • 2012-08-28
  • 1970-01-01
  • 2018-12-16
相关资源
最近更新 更多