How to remove <style ***** </style>

Discussion in 'PHP' started by redhits, Feb 17, 2010.

  1. #1
    Hello, can somebody give me an example of how to write an preg_replace, that will complete remove everything inside an <style .... </style> tag


    everything i found on the net is not working :( thanks
     
    redhits, Feb 17, 2010 IP
  2. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #2
    preg_replace("/<style[^>]*?>(.*?)<\/style>/Uis",'',$html);
    PHP:
     
    danx10, Feb 17, 2010 IP
  3. koko5

    koko5 Active Member

    Messages:
    394
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    70
    #3
    @danx10 < and > must be escaped too :)
     
    koko5, Feb 18, 2010 IP
  4. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #4
    Since when ?
     
    joebert, Feb 18, 2010 IP
  5. koko5

    koko5 Active Member

    Messages:
    394
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    70
    #5
    OK, not must but better maybe
    preg_quote
    If we have $MyRegExp="/<style>/"; to simplify:
    
    $MyRegExp="/<style>/";
    echo preg_quote($MyRegExp)."\n";
    
    PHP:
    Echoed
    However, this simple example gave warning and I'll check why when I've time for this.
    Regards
     
    koko5, Feb 18, 2010 IP
  6. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #6
    It's certainly odd, that's for sure. I've never had to escape < or > and I've used the preg_ functions more times than I can count. Sure enough though, preg_quote escapes those characters.
     
    joebert, Feb 18, 2010 IP
  7. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #7
    sorry, meant:

    preg_replace("/<style[^>]*?>(.*?)<\/style>/Uis",'$1',$html);
    PHP:
    Posted that last night and was tired.

    @koko5 test this:

    <?php
    
    $html = <<<HTML
    <style>
    blahblah...
    </style>
    HTML;
    
    echo preg_replace("/<style[^>]*?>(.*?)<\/style>/Uis",'$1',$html);
    
    ?>
    PHP:
     
    danx10, Feb 18, 2010 IP