Pergunta

I need to get rid of all the HTML tags in a description that gets returned from the Petfinder API, but I can't seem to figure out how.

$data['description'] holds the returned description. What I can see in the source file is something like this...

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

I've tried using strip_tags, Ive tried it with html_entity_decode, but the tags won't go away!

I need to strip the tags so that I can truncate the description..

Anyone have any ideas?

$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>";
Foi útil?

Solução

The problem is that you are using the original input again half-way your processing:

$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."...";

Apart from that, @redreggae is right, you only need strip_tags.

Outras dicas

the problem is you first run htmlentities. After this strip_tags can't work anymore.

Simply do:

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

The hardcore way:

$doc = new DOMDocument();
$doc->loadHTML('<div>some text that gets returned</div>');
$items = $doc->getElementsByTagName('div');
$content = $items->item(0)->nodeValue;
echo $content;
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top