Using a simple regular expressions to check a filename has ".php" on

Using a simple regular expressions to check a filename has ".php" on

am 08.01.2008 14:07:31 von bizt

Hi,

Im trying to write a regular expression in PHP to check if a submitted
filename has .php on the end of it. For example, filename.html woulod
return false whereas filename.php would return true.

My code is:

if (ereg ("$.php", $filename))
return true;
else
return false;

Obviously my expression is wrong, could someone please tell me what Im
doing wrong. Thanks

Burnsy

Re: Using a simple regular expressions to check a filename has ".php" on the end of it

am 08.01.2008 14:09:55 von luiheidsgoeroe

On Tue, 08 Jan 2008 14:07:31 +0100, bizt wrote:

> Hi,
>
> Im trying to write a regular expression in PHP to check if a submitted
> filename has .php on the end of it. For example, filename.html woulod
> return false whereas filename.php would return true.
>
> My code is:
>
> if (ereg ("$.php", $filename))
> return true;
> else
> return false;
>
> Obviously my expression is wrong, could someone please tell me what Im
> doing wrong. Thanks

1. Use PCRE not POSIX, the ereg* function will even disappear in PHP6 it
seems.
2. preg_match('/\.php$/',$filename)
--
Rik Wasmus

Re: Using a simple regular expressions to check a filename has ".php"on the end of it

am 08.01.2008 14:14:29 von Jerry Stuckle

bizt wrote:
> Hi,
>
> Im trying to write a regular expression in PHP to check if a submitted
> filename has .php on the end of it. For example, filename.html woulod
> return false whereas filename.php would return true.
>
> My code is:
>
> if (ereg ("$.php", $filename))
> return true;
> else
> return false;
>
> Obviously my expression is wrong, could someone please tell me what Im
> doing wrong. Thanks
>
> Burnsy
>

A regex is overkill for a simple string. strcmp() is better in this case.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

Re: Using a simple regular expressions to check a filename has ".php" on the end of it

am 08.01.2008 14:33:42 von Michael Fesser

..oO(bizt)

>Im trying to write a regular expression in PHP to check if a submitted
>filename has .php on the end of it. For example, filename.html woulod
>return false whereas filename.php would return true.
>
>My code is:
>
>if (ereg ("$.php", $filename))
> return true;
>else
> return false;
>
>Obviously my expression is wrong, could someone please tell me what Im
>doing wrong. Thanks

return pathinfo($filename, PATHINFO_EXTENSION) == 'php';

Micha

Re: Using a simple regular expressions to check a filename has ".php"on the end of it

am 08.01.2008 14:41:44 von Courtney

Michael Fesser wrote:
> .oO(bizt)
>
>> Im trying to write a regular expression in PHP to check if a submitted
>> filename has .php on the end of it. For example, filename.html woulod
>> return false whereas filename.php would return true.
>>
>> My code is:
>>
>> if (ereg ("$.php", $filename))
>> return true;
>> else
>> return false;
>>
>> Obviously my expression is wrong, could someone please tell me what Im
>> doing wrong. Thanks
>
> return pathinfo($filename, PATHINFO_EXTENSION) == 'php';
>
> Micha

Neat.

Re: Using a simple regular expressions to check a filename has ".php"on the end of it

am 08.01.2008 20:54:56 von Jeremy

Michael Fesser wrote:
> .oO(bizt)
>
>> Im trying to write a regular expression in PHP to check if a submitted
>> filename has .php on the end of it. For example, filename.html woulod
>> return false whereas filename.php would return true.
>>
>> My code is:
>>
>> if (ereg ("$.php", $filename))
>> return true;
>> else
>> return false;
>>
>> Obviously my expression is wrong, could someone please tell me what Im
>> doing wrong. Thanks
>
> return pathinfo($filename, PATHINFO_EXTENSION) == 'php';
>
> Micha

Thanks for this. I had never noticed that pathinfo can return a string
rather than an array.

Jeremy