SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,443
    Mentioned
    82 Post(s)
    Tagged
    3 Thread(s)

    Regex problem with a #

    I have written a simple regular expression to check input but have a problem with the # I have a feeling that I have another problem in that I need to limit the amount of letters in the hex value to a maximium of 6 but am not sure how.
    Here is the code can somebody let me know why the # is causing a problem and any other suggestions would be helpful.

    All my good inputs get through except #ffffff and now as I have added the a-f this rgb(2a,255,255) bad input gets through as well. I could always do extra tests after this one to check that only an input starting with a # has letters.

    PHP Code:
    <?php
    /*
    Possible good inputs
    rgb(255,255,255) - OK
    rgba(255,255,255,1.0) - OK
    rgb(100%,100%,100%) - OK 
    #ffffff - No
    hsl(0,0,255)  - OK
    hsl(0%,0%,100%)  - OK
    hsb(0,0,255)  - OK
    hsb(0%,0%,100%)  - OK
    cmyk(0,0,0,0)  - OK
    cmyk(0%,0%,0%,0%) - OK

    Possible bad inputs
    rgb(2a,255,255) - Yes
    rgbd(2,255,255) - No
    (2,255,255) - No
    2,255,255 - No
    */

    //$color = $_GET['color'];

    // Hardcoaded for testing
    $color "rgb(2a,255,255)";

    // Change all the text to lowercase this will reduce the amount of options
    $color strtolower$color );

    // Check for valid input
    // Returns 1 if a match is found and 0 otherwise
    if( preg_match "/^(rgb|rgba|hsl|hsb|cmyk|gray)\([a-f0-9\.,%#]+\)/"$color$match)){
    echo 
    "Wahoo";
    }
    else echo 
    "Blah";
    ?>

  2. #2
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,443
    Mentioned
    82 Post(s)
    Tagged
    3 Thread(s)
    Of course it wont match it does not have any () in it and does not start with a rgb or rgba or hsl or hsb or cmyk or gray

    Nothing at all to do with the #

    Wife really impressed that I have got out of bed to post on a forum !!!!!!
    Last edited by Rubble; Oct 14, 2009 at 14:54. Reason: Edit spelling


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
  •