scraping <style type="text/css"> with preg_match

Discussion in 'PHP' started by brianj, May 10, 2009.

  1. #1
    Hi, simple problem again:

    <?php
       $data=file_get_contents("http://www.domain.com/"); 
        preg_match('/<style(.*)?>(.*) ? <\/style>/', $match );
        print ($match);
    ?>
    PHP:
    I try to print out the content inside <style type="text/css">xxx</style>

    Anyone know what's wrong with this code`?

    Thanks,
     
    brianj, May 10, 2009 IP
  2. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #2
    #<style[^>]+>(.*?)</style>#
     
    EricBruggema, May 10, 2009 IP
  3. JDevereux

    JDevereux Peon

    Messages:
    50
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    preg_match('/<style[^>]+>(.*?)<\/style>/', $data, $match);
    print_r($match);
    PHP:
     
    JDevereux, May 10, 2009 IP
  4. brianj

    brianj Member

    Messages:
    50
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #4
    Hey, thanks alot to both of you.. now it outputs: Array ( )

    //this is what i try to do:
    <?php
    $data = file_get_contents("http://www.twitter.com/username/");
    preg_match('/<style[^>]+>(.*?)<\/style>/', $data, $match);
    print_r($match);
    ?>
    
    PHP:


    Thanks:)
     
    brianj, May 10, 2009 IP
  5. koko5

    koko5 Active Member

    Messages:
    394
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    70
    #5
    Hi,

    try this
    preg_match('#\<style(.*)?\stype=[\'\"]text\/css[\'\"](.*)?\>(.*)?\<\/style\>#i',$data,$match);
    PHP:
    Regards
     
    koko5, May 10, 2009 IP
  6. JDevereux

    JDevereux Peon

    Messages:
    50
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    You're right, that didn't work. This is how I usually scrape stuff:

    $html = file_get_contents("http://www.twitter.com/username/");
    
    $dom = new DOMDocument();
    
      @$dom->loadHTML($html);
            
      $styles = $dom -> getElementsByTagName('style');
    
      foreach ($styles as $style) {
        if ($style->getAttribute('type') == "text/css") {
        $css = $style -> firstChild -> data;
        echo $css;
        }
        }
    PHP:
     
    JDevereux, May 10, 2009 IP
  7. brianj

    brianj Member

    Messages:
    50
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #7
    Yes! this one works great, thanks!!
     
    brianj, May 11, 2009 IP
  8. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #8
    #<style[^>]+>(.*?)</style>#

    this works fine if you add ISM to the last # char! then it checks multiple lines! :)

    #<style[^>]+>(.*?)</style>#ism
     
    EricBruggema, May 11, 2009 IP