Regex

Discussion in 'PHP' started by amorph, Oct 4, 2007.

  1. #1
    Any idea how to extract all the files (1.css, 2.css, 3.css) from this string using regex?

    $string = '@import url("1.css"); @import url(2.css); @import "3.css"; @import url("4.css") all;';
    PHP:

     
    amorph, Oct 4, 2007 IP
  2. intoex

    intoex Peon

    Messages:
    414
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #2
    preg_match_all("/url(\"(\d+.css)\"/",$out); - i thins so
     
    intoex, Oct 4, 2007 IP
  3. amorph

    amorph Peon

    Messages:
    200
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    neah...does not work at all..

    I got very close but still far away :D

    preg_match_all ( '/@import[^>]+\p{P}(.*?).css/si', $string, $sheet );
    PHP:
     
    amorph, Oct 4, 2007 IP
  4. amorph

    amorph Peon

    Messages:
    200
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Someone....?! I really need this :p
     
    amorph, Oct 4, 2007 IP
  5. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #5
    
    preg_match_all('/import\s+(?:url\()?["\']?([^"\'\)]+)/i', $string, $css);
    
    print_r($css[1]);
    
    PHP:
     
    nico_swd, Oct 4, 2007 IP
  6. amorph

    amorph Peon

    Messages:
    200
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Of course it works :). If my project will convert, I'll have to split with you :). Thanks again nico.
     
    amorph, Oct 4, 2007 IP
  7. amorph

    amorph Peon

    Messages:
    200
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    hey nico...any idea why your regex fails to extract in this situation (a valid css import):



    $str = '@import: url(\'test.css\');';
    function searchCSSImports ( $str )
    {
    		preg_match_all ( '/import[^>]+(?:url\()?["\']?([^"\'\)]+)/i', $str, $sheet );
    		return $sheet [ 1 ];
    }
    	
    
    echo '<pre>';
    print_r ( searchCSSImports ( $str ) );
    echo '</pre>';
    PHP:
     
    amorph, Oct 6, 2007 IP
  8. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #8
    
    '/@import\s*:?\s*(?:url\()?["\']?([^"\'\)]+)/i'
    
    PHP:
    Works for me with these examples. (Didn't test others, if there are any)
    
    $str = '
    	@import: url(\'test.css\');
    	@import: \'test2.css\';
    	@import url("1.css"); 
    	@import url(2.css);
    	@import "3.css";
    	@import url("4.css") all;';
    
    PHP:
     
    nico_swd, Oct 6, 2007 IP
  9. amorph

    amorph Peon

    Messages:
    200
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Thank you very much for the speedy reply.
     
    amorph, Oct 6, 2007 IP