|
Posted by Colin Fine on 10/16/06 21:30
mantrid wrote:
>
> "peter" <submit@flexiwebhost.com> wrote in message
> news:egv0dc$34o$1@emma.aioe.org...
>>> Found this piece of code using preg_match to check file types during
>>> upload
>>> of files.
>>> $allowed_file_types = "(jpg|jpeg|gif|bmp|png)";
>>> preg_match("/\." . $allowed_file_types . "$/i",
>>> $_FILES['uploadedfile']["name"])
>>>
>>> I understand the basic preg_match but am confused as to how the string
>>> pattern part is working i.e.
>>> "/\." . $allowed_file_types . "$/i"
>> the regular expression equates to the following:-
>>
>> "/\.(jpg|jpeg|gif|bmp|png)$/i"
>>
>> basically it is saying that the string MUST contain a . (which has been
>> escaped by the slash) plus jpg OR jpeg OR gif OR bmp OR png. AS you see
> the
>> | means OR when enclosed in brackets like it currently is. The $ states
> that
>> the match must be at the end of the string (or in this case the file name)
>> and the /i staes that the match should be case insensitive.
>>
>>
>>
>
>
(top posting corrected)
> thanks that explains a lot.
> if i wanted to make it case insensitive should I simply remove the /i ?
> also I thought || was 'or', is this special only to preg_match?
> also
> why the /\. and not just \.
>
> Ian
>
Many languages distinguish '|' bitwise OR from '||' boolean OR. It had
never occurred to me before that '|' in regexp had any connection with
these operators: I guess therefore that the answer is that there is no
need to distinguish two different kinds of OR so you might as well use a
single character.
The / .. / pair are the delimiters of the pattern. I think you can use
any character that doesn't occur in the pattern (and I suspect that, as
in Perl, you can use matched characters like '{' and '}' if they don't
occur in the pattern, though I can't quickly find a reference to say so).
We talk of the modifier as being '/i', but really it's just 'i'
immediately following the closing delimiter.
Colin
[Back to original message]
|