Preg_match_all help

Discussion in 'PHP' started by samirkumardas, Jul 23, 2008.

  1. #1
    I need to extract all href from external css link e.g.

    <link rel="stylesheet" href="main.css" type="text/css"> OR
    <link rel="stylesheet" type="text/css" href="main.css"> OR
    <link href="main.css" type="text/css" rel="stylesheet" > OR

    from any of combination of insides attributes. Simply I just need to find all external css location,


    Expected Result:

    main.css


    Pls help
     
    samirkumardas, Jul 23, 2008 IP
  2. matthewrobertbell

    matthewrobertbell Peon

    Messages:
    781
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    0
    #2
    It seems you need a lot of regular expressions, I think it would be very useful to learn them, instead of posting whenever you need one.
     
    matthewrobertbell, Jul 23, 2008 IP
  3. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #3
    ^ I agree...

    Anyway...
    
    preg_match_all('~<link[^>]+(?:(?:href="([^"]+)"|type="text/css")\s*){2}[^>]*>~', $html, $matches);
    
    echo '<pre>', print_r($matches[1], true), '</pre>';
    
    PHP:
     
    nico_swd, Jul 23, 2008 IP
  4. samirkumardas

    samirkumardas Banned

    Messages:
    123
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I am agreed too. :) thanks for help.

    I think ,I will help others next time when they need reg exp problem.

    Thanks
     
    samirkumardas, Jul 23, 2008 IP
  5. samirkumardas

    samirkumardas Banned

    Messages:
    123
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Hi,


    Does not work. Since all pattern seems to same to me, I cant find the error.

    What the the problems?
     
    samirkumardas, Jul 23, 2008 IP
  6. matthewrobertbell

    matthewrobertbell Peon

    Messages:
    781
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    0
    #6
    "(?:" - care to explain please nico?
     
    matthewrobertbell, Jul 23, 2008 IP
  7. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #7
    I've tried it, and it works just fine for me:
    
    $html = '<link rel="stylesheet" href="main3.css" type="text/css"> OR
    <link rel="stylesheet" type="text/css" href="main5.css"> OR
    <link href="main4.css" type="text/css" rel="stylesheet" > OR';
    
    
    preg_match_all('~<link[^>]+(?:(?:href="([^"]+)"|type="text/css")\s*){2}[^>]*>~', $html, $matches);
    
    echo '<pre>', print_r($matches[1], true), '</pre>';
    
    PHP:
    Gives me:
    
    Array
    (
        [0] => main3.css
        [1] => main5.css
        [2] => main4.css
    )
    
    Code (markup):
    How did you try it?

    That means this group won't be captured in the matches array.
     
    nico_swd, Jul 23, 2008 IP
  8. samirkumardas

    samirkumardas Banned

    Messages:
    123
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Hi nico,

    Thanks for the help. Your expression does not give result. Could you please look at?
     
    samirkumardas, Jul 23, 2008 IP
  9. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #9
    I did... but I can't help unless you answer my question...

     
    nico_swd, Jul 23, 2008 IP
  10. samirkumardas

    samirkumardas Banned

    Messages:
    123
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Hi Nicho,
    My html was as

    $html='<meta name="Author" content="Bdjokes.com" />
    <meta name="Rating" content="General" />
    <title>bdjokes :: A collection of jokes in bangla(Bangla Jokes)</title>
    <link href="css.css" rel="stylesheet" type="text/css">
    <link href="style.css" rel="stylesheet" type="text/css">
    </head>';
    preg_match_all('~<link[^>]+(?:(?:href="([^"]+)"|type="text/css")\s*){2}[^>]*>~', $html, $matches);
    
    echo '<pre>', print_r($matches[1], true), '</pre>';
    PHP:

    but no result...



    Thanks
     
    samirkumardas, Jul 23, 2008 IP
  11. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #11
    Works for me:
    
    '~<link[^>]+(?:\s*(?:href="([^"]+)"|type="text/css"|rel="stylesheet")\s*){3}[^>]*>~'
    
    PHP:
     
    nico_swd, Jul 23, 2008 IP
  12. matthewrobertbell

    matthewrobertbell Peon

    Messages:
    781
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Thank for explaining + repped.
     
    matthewrobertbell, Jul 23, 2008 IP
  13. samirkumardas

    samirkumardas Banned

    Messages:
    123
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #13
    It works!!

    Thanks nico, for the help.........

    Thanks again
     
    samirkumardas, Jul 23, 2008 IP