我需要删除从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>";
答案 0 :(得分: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;
答案 1 :(得分:2)
问题是您在处理中途再次使用原始输入:
$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
。