Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4

Thread: Preg Match Help

  1. #1
    Regular Coder
    Join Date
    Apr 2008
    Posts
    685
    Thanks
    15
    Thanked 105 Times in 104 Posts

    Preg Match Help

    Hi,

    I need to use a preg match to check if a color entered is a valid hexidecminal color. I found the following off a website:

    PHP Code:
    if(preg_match('/^#[a-f0-9]{6}$/i'$color)) //hex color is valid
    {


    This works great, but a hex color can also be 3 characters, and the preg match in question does not allow for this.

    I have no idea how preg-matches work or how easy they are to code, but can this be amended to allow for 3 characters also?

  • #2
    Super Moderator
    Join Date
    Feb 2009
    Location
    England
    Posts
    539
    Thanks
    8
    Thanked 63 Times in 54 Posts
    '/^#[a-f0-9]{6}$|^#[a-f0-9]{3}$/i'

    | means OR.
    lamped.co.uk :: Design, Development & Hosting
    marcgray.co.uk :: Technical blog

  • Users who have thanked Lamped for this post:

    Scriptet (10-28-2009)

  • #3
    Regular Coder
    Join Date
    Apr 2008
    Posts
    685
    Thanks
    15
    Thanked 105 Times in 104 Posts
    Thanks ComputerX for the quick, and effective reply!!

    If anybody has a great tutorial on learning about preg matches please do post...because I need to learn this for myself also.

    For instance I now need to write one which checks if the field is in the form 200px or 200%, obviously the 200 can be any number, but have no idea on how to go about it.
    Last edited by Scriptet; 10-28-2009 at 04:02 PM.

  • #4
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,555
    Thanks
    57
    Thanked 148 Times in 147 Posts
    Code:
    preg_patch("#^([0-9]*)((?:%)|(?:px))$#is", $str, $array);
    
    $array[1] = number value
    $array[2] = px | %
    
    ^ = begging of string
    $ = end of string
    
    ("this size is 200px!") return false
    ("200px!") return false
    ("!200px") return false
    ("200px") return true
    
    () group together and access value from array
    (?:) group together but leave out from accessible array
    | means or
    [] means either
    [0-9] figure between 0 and 9
    * means 0 or more times
    
    EDIT: use of + would be more appropriate
    preg_patch("#^([0-9]+)((?:%)|(?:px))$#is", $str, $array);
    
    + means one or more times
    Last edited by Phil Jackson; 10-28-2009 at 04:44 PM.
    Website Design Mansfield
    PHP Code:
    function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
    echo 
    I_LOVE(); 


  •  

    Posting Permissions

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