By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,661 Members | 779 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,661 IT Pros & Developers. It's quick & easy.

Using a simple regular expressions to check a filename has '.php' onthe end of it

P: n/a
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
Jan 8 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On Tue, 08 Jan 2008 14:07:31 +0100, bizt <bi******@yahoo.co.ukwrote:
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
Jan 8 '08 #2

P: n/a
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.
js*******@attglobal.net
==================

Jan 8 '08 #3

P: n/a
..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
Jan 8 '08 #4

P: n/a
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.
Jan 8 '08 #5

P: n/a
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
Jan 8 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.