SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,223
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    verifying color code

    Code:
    <?php
    $myColor="0000ff";
    ?>
    I have a variable named $myColor like the above.

    I like to verify whether the value of $myColor is color code or not.

    The following code doesn't work correctly, but I hope it shows what I want.
    Code:
    code1
    
    <?php
    $myColor="0000ff";
    
    if ( is_colorCode($myColor) )
    {echo yes;}
    else
    { echo no;}
    ?>
    
    result1
    
    yes


    Code:
    code2
    
    <?php
    $myColor="0xy0000";
    
    if ( is_colorCode($myColor) )
    {echo yes;}
    else
    { echo no;}
    ?>
    
    result2
    
    no

  2. #2
    Non-Member
    Join Date
    Apr 2011
    Location
    no fixed address
    Posts
    851
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you can use a regex to see if $myColor contains only 0-9 and/or a-f charachters

  3. #3
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,824
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    /^[0-9a-f]{6}$/i
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  4. #4
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,223
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by felgall View Post
    /^[0-9a-f]{6}$/i
    I am afraid that the code below which is one of my trials applying your code doesn't work correctly.
    Code:
     if ( $myColor contains /^[0-9a-f]{6}$/i )
    {echo yes;}
    else
    {echo no;}

  5. #5
    SitePoint Wizard bronze trophy Immerse's Avatar
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    1,661
    Mentioned
    7 Post(s)
    Tagged
    1 Thread(s)
    That's not PHP. There is no 'contains' in PHP.

    PHP Code:
    <?php

    $myColor
    ="0000ff";

    if(
    preg_match('/^[0-9a-f]{6}$/i'$myColor)) {
        echo 
    "Match";
    } else {
        echo 
    "No match";
    }

    ?>

  6. #6
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Isn't a 3 digit Hex representation of a colour also valid?
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  7. #7
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    PHP Code:
    $string '000fff';
    $len strlen($string);

        if (
    ctype_xdigit($string) && ($len == || $len == 6) ) 
            echo 
    "The string $string is a valid color"

  8. #8
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Very nice Cups, here's how the Imagine library validates an incoming colour declaration.

    https://github.com/avalanche123/Imagine/blob/develop/lib/Imagine/Image/Color.php#L176

    T
    ruth be told, I guess it only really ensures the value supplied can be transformed into a colour, rather than validating the value.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  9. #9
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,223
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thank you to All.

  10. #10
    SitePoint Wizard bronze trophy Immerse's Avatar
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    1,661
    Mentioned
    7 Post(s)
    Tagged
    1 Thread(s)
    A 3 digit length is also valid, yes.
    Cups, your version would allow stuff like.... #ayh9ep, right? Which isn't valid. The Imagine library doesn't check that either.

    Right... /^[0-9a-f]{3,6}$/i' ... No, that'll allow 4 and 5 character colour codes too. And there my regex skillz fail.

  11. #11
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    "ayh9ep" fails the ctype_xdigit() test for me? (Win32, 5.2.6) as does "#ayh9ep"?

  12. #12
    SitePoint Wizard bronze trophy Immerse's Avatar
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    1,661
    Mentioned
    7 Post(s)
    Tagged
    1 Thread(s)
    Ah, my bad! I hadn't noticed the 'x' in ctype_xdigit.

    My apologies, good sir.


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
  •