SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict kiltman's Avatar
    Join Date
    Mar 2004
    Location
    scotland
    Posts
    235
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP Preg_Match problem

    Hey guys,

    having some problems with the following

    $_GET['matricnumber'] = "u0404695"
    $_GET['computerid'] = "C11-01"

    these values are case insensitive, e.g. computerid may be c11-01 or C11-01, and matricnumber may be U0404695 or u0404695

    i'm trying to use the following to validate that the values are set properly, however it doesn't work due to the regular expressions (never really used them before)

    if (preg_match("/^[u][0-9]$/", $_GET['matricnumber'])) {
    echo "<p>match is true</p>";
    } else {
    echo "<p>match is untrue</p>";
    }

    if(preg_match("/^[a-zA-Z0-9][-][a-zA-Z0-9]$/", $_GET['computerid'])) {
    echo "<p>match is true</p>";
    } else {
    echo "<p>match is untrue</p>";
    }

    any help would be great

    many thanks

    Paul

  2. #2
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,935
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You will need to properly describe the patterns that are allowed.

  3. #3
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you can use the "i" modifier to make the match case insensitive

    can you tell us a bit more about matricnumber and computerid

    for example
    matricnumber always starts with a u or U, and is always followed by 7 digits?


    you can specify how many times a certain character is supposed to match
    for example
    Code:
    /^[u][0-9]{5,7}]$/i
    that would match a case insensitive u, followed by a minimum of 5, max of 7 digits

  4. #4
    SitePoint Addict kiltman's Avatar
    Join Date
    Mar 2004
    Location
    scotland
    Posts
    235
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks guys, the patterns are as follows

    matric number can start with an optional u, and then has a serious of numbers for example

    u0404695
    0404695
    02343564325

    computer id is in the format of

    C11-01

    C11 is the room name, and always starts with a letter first, and then two numbers and is then always followed by a - e.g. C11-

    the 01 at the end is the computer id for that room, and is always a 2 digit number e.g. 01

    the final computer name is as C11-01

    both of these can be case insensitive e.g. u0404695 or U0404695, c11-01 or C11-01

    hope that helps clear it up a bit

    Again thanks

  5. #5
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This:

    /^[u][0-9]$/

    matches something like u1 or u9. This:

    [a-zA-Z0-9][-][a-zA-Z0-9]$/

    matches something like a-9 or 0-a or 2-2. The brackets only represent one character where the character can be any of the characters in the brackets.

    Try:

    1) /u?[0-9]+/i

    '?' means the preceding character will be present 0 or 1 time. '+' means the preceding character is present 1 or more times--remember everything in brackets represents one character. The trailing 'i' means case insensitive.

    2)/^[a-z][0-9][0-9]-[0-9][0-9]$/i
    Last edited by 7stud; Mar 9, 2006 at 12:42.

  6. #6
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,935
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $mat_num '@^[a-z][0-9]{2}[-][0-9]{2}$@i';
    $com_id '@^[a-z]?[0-9]+$@i'


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
  •