Strip Tags and everything in between

前端 未结 5 1295
南方客
南方客 2020-11-29 08:30

How can i strip

including this content

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

相关标签:
5条回答
  • 2020-11-29 09:06

    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
    
    0 讨论(0)
  • 2020-11-29 09:12

    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.

    0 讨论(0)
  • 2020-11-29 09:15

    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))
    --------------------------------------------------------------------------------
      >                        '>'
    
    0 讨论(0)
  • 2020-11-29 09:16

    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();
    
    0 讨论(0)
  • 2020-11-29 09:29

    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.

    0 讨论(0)
提交回复
热议问题