Remove certain HTML tag with its content from string then strip all html tags

// create new variable called $dom that will hold the html $dom = new DOMDocument(); // taking care of the UTF-8 issues $news_text= mb_convert_encoding($news_text, 'HTML-ENTITIES', "UTF-8"); // load the HTML to the $dom object $dom->loadHTML($news_text); $dom->preserveWhiteSpace = false; // getting all spans inside the dom element $elements = $dom->getElementsByTagName('span'); $spans = array(); // removing the spans foreach($elements as $span) { $spans[] = $span; } foreach($spans as $span) { $span->parentNode->removeChild($span); } // saving the $dom after removing the spans in the old variable called $news_text $news_text = $dom->saveHTML(); // removing the rest of the tags and saving it to the old variable called $news_text $news_text = strip_tags($news_text);

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.