SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Addict jasongr's Avatar
    Join Date
    Jul 2004
    Location
    usa
    Posts
    371
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    bug in the regular expression

    Hello people

    I would like to use regular expression to check if a string contains the definition of background-image:url(<file>.png) attribute

    that is in the following strings the attribute will be found:
    style="background-image: url('test.png')"
    style="background-image: url('/Folder/test.png')"
    style="background-image: url('./folder/Test.png')"

    I tried the following but it doesn't work:
    PHP Code:
    preg_match_all('/background-image:\s*url\(\'(.*\.png)\'\);/Uis',$s$matches); 
    However count($matches[0]) is always zero

    can anyone see what is wrong with the regular expression, or maybe something is wrong in my examples

    thanks

  2. #2
    SitePoint Enthusiast Viper_SB's Avatar
    Join Date
    Aug 2003
    Location
    Canada
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's because you have a ; at the end of your regex and none in your text, use this regex (in my opinon don't use the U modifier but use ? through out, but that's just how I'd do it)

    for your code just add a ? after the ; in the regex

    how I'd do it
    PHP Code:
    preg_match_all('/background-image:(\s*)?url\(\'.+?\.png\'\);?/is'$s$matches); 

  3. #3
    SitePoint Addict jasongr's Avatar
    Join Date
    Jul 2004
    Location
    usa
    Posts
    371
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for the suggestion


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •