Strip Tags and everything in between

前端 未结 5 1326
南方客
南方客 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:15

    If you want to strip ALL tags and including content:

    $yourString = 'Hello 
    Planet
    Earth. This is some sample 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))
    --------------------------------------------------------------------------------
      >                        '>'
    

提交回复
热议问题