This is some text
This is the button

Title
Here is some more text and this is a link

'; $dom = new DOMDocument(); $dom->loadHTML($xhtml); function clean($element, $allowed, $stripped, $prefix = ''){ if(!is_array($allowed) || ! is_array($stripped)) return; if(!$element)return; $toDelete = array(); foreach($element->childNodes as $child){ if(!isset($child->tagName))continue; $n = $child->tagName; if ($n && !in_array($n, $allowed) && !in_array($n, $stripped)){ $toDelete[] = $child; continue; } if($n && in_array($n, $stripped)){ $attr = array(); foreach($child->attributes as $a) $attr[] = $a->nodeName; foreach($attr as $a) $child->removeAttribute($a); } clean($child, $allowed, $stripped, $prefix.' '); } foreach ($toDelete as $del) $element->removeChild($del); } $body = $dom->getElementsByTagName('body')->item(0); clean($body, array('img', 'a'), array('p', 'br', 'b')); echo preg_replace('#^.*?(.*?).*$#s', '$1', $dom->saveHTML($body));