Rajib Deb
Rajib Deb

Reputation: 51

How to remove specific html tag and it's content from string in php

I have some string values like this example,

1) '7-66, 7-65' 2) '610-7, 6-0, 6-2'

I want below output, 1) '7-6, 7-6' 2) '6-7, 6-0, 6-2'

I have written below php code which is not working properly,

$score = '7-6<sup>6</sup>, 7-6<sup>5</sup>';
// $score ='6<sup>10</sup>-7, 6-0, 6-2';

while(strpos($score, '<sup>') !== false) {

    $tag_start  = strpos($score, '<sup>');

    $tag_end = strpos($score, '</sup>') + 5;

    $sub_str = substr($score, $tag_start, $tag_end);

    $score = str_replace($sub_str, '', $score);
    $score = str_replace(' ', '', $score);
}

echo $score;

Upvotes: 1

Views: 451

Answers (1)

feeela
feeela

Reputation: 29932

One reliable way to do that, is to use a XML parser and its methods, e.g. SimpleXML. The advantage over string replacing with fixed string lengths (strpos(…) + 5) or using regular expressions, is that you really can find all occurrences of a specified element, even if it bears attributes.

<?php

$score = '7-6<sup>6</sup>, 7-6<sup>5</sup>, 6<sup>10</sup>-7, 6-0, 6-2';

/* Add a temporary wrapper element, so that any string input may be parsed as valid XML */
$xml = new SimpleXMLElement(sprintf('<html>%s</html>', $score));

/* Find the elements to be deleted using XPATH */
foreach ($xml->xpath('sup') as $sup) {
    /* Remove this node using unset() */
    unset($sup[0]);
}

/* Remove the temporary wrapper and the XML header, which is printed by asXML() */
$xmlOutput = trim(str_replace(['<?xml version="1.0"?>', '<html>', '</html>'], '', $xml->asXML()));

var_dump($xmlOutput);

See also https://en.wikipedia.org/wiki/XPath

Another option would be to use strip_tags() and list all allowed tags in the second attribute.

Upvotes: 1

Related Questions