I need help with preg_match.

The user will upload a file to my server but I want the filename to look in a certain way. For example, this is a correct filename:

SES 00001.TIF

The first part (SES) can contain the following characters: ABCDEFGHIJKLMNOPQRSTUVWXYZ

The second part must be a space.

The third part is a number with 5 digits. 0123456789

The fourth part must be .TIF

If the filename passes all 4 tests it's a correct filename. I'm sure I can do this with preg_match but how?

    You could use something along these lines:

    if (preg_match('/^[A-Z]{3} \d{5}\.TIF$/', $filename))
    {
        // pass
    }
    else
    {
        // fail
    }

    I am assuming that the first part must contain exactly three uppercase letters.

      On a side note for any reg-ex work i always use "Regex coach" for testing my expressions, you can download it from :

      http://www.weitz.de/regex-coach/

      I've learned nearly all i know about regular expressions by simply putting test tex into the middle box, and playing with the expression in the top box and observing what happens.

      It's a tool that with out doubt should be in all programmers tool boxes.

      Cheers

      Shawty

        Write a Reply...