tags:

views:

114

answers:

5

How can i strip <h1>including this content</h1>

I know you can use strip tags to remove the tags, but i want everything in between gone as well.

Any help would be appreciated.

+1  A: 

Try this:

preg_replace('/<h1[^>]*>([\s\S]*?)<\/h1[^>]*>/', '', '<h1>including this content</h1>');

Example:

echo preg_replace('/<h1[^>]*>([\s\S]*?)<\/h1[^>]*>/', '', 'Hello<h1>including this content</h1> There !!');

Output:

Hello There
Sarfraz
HTML allows plain `>` in attribute values.
Gumbo
+6  A: 

As you’re dealing with HTML, you should use an HTML parser to process it correctly. You can use PHP’s DOMDocument and query the elements with DOMXPath, e.g.:

$doc = new DOMDocument();
$doc->loadHTML($html);
$xpath = new DOMXPath($doc);
foreach ($xpath->query('//h1') as $node) {
    $node->parentNode->removeChild($node);
}
$html = $doc->saveHTML();
Gumbo
+1 Use a parser here. Do it right once, and won't have to revisit when you (or another dev, or a client using a WYSIYWG editor) invalidates the regex.
alex
This look like a good solution however i only want to include the listing h1's not the actual page h1, if that makes sense. Ive noted this for other use though thank you.
Andy
@Andy just change the xPath selector to match only h1 inside a containing element.
alex
Ahh ok, didnt realise you could do that, thank you.
Andy
+1  A: 

If you want to strip ALL tags and including content:

$yourString = 'Hello <div>Planet</div> Earth. This is some <span class="foo">sample</span> content!';
$regex = '/<[^>]*>[^<]*<[^>]*>/';
echo preg_replace($regex, '', $yourString);
#=> Hello  Earth. This is some  content!

HTML attributes can contain < or >. So, if your HTML gets too messy this method will not work and you'll need a DOM parser.


Regular Expression Explanation

NODE                     EXPLANATION
--------------------------------------------------------------------------------
  <                        '<'
--------------------------------------------------------------------------------
  [^>]*                    any character except: '>' (0 or more times
                           (matching the most amount possible))
--------------------------------------------------------------------------------
  >                        '>'
--------------------------------------------------------------------------------
  [^<]*                    any character except: '<' (0 or more times
                           (matching the most amount possible))
--------------------------------------------------------------------------------
  <                        '<'
--------------------------------------------------------------------------------
  [^>]*                    any character except: '>' (0 or more times
                           (matching the most amount possible))
--------------------------------------------------------------------------------
  >                        '>'
macek
HTML allows plain `>` in attribute values.
Gumbo
Gumbo, you beat me to this. I was just making an amendment :)
macek
Wouldn't this leave you without any content at all, given that all HTML is inside the root node?
Gordon
A: 

Hello,

You also use strip_tags to remove the tags and also everything in between..

$html contain your html or php from where you want to remove the tags.

strip_tags($html,"");

Try this i think this will work for you.

Kanak Vaghela
A: 

You could use an XSLT stylesheet and match all tags to themselves except for the h1 tag which would be matched to the empty string, and then apply it to your document. Might be a bit too heavy-weight for doing something as simple as this though.

wasatz