473,406 Members | 2,620 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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

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
5 4420
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
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
..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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: YoBro | last post by:
Hi I have used some of this code from the PHP manual, but I am bloody hopeless with regular expressions. Was hoping somebody could offer a hand. The output of this will put the name of a form...
8
by: laredotornado | last post by:
Hi, I'm using PHP 4 and trying to parse through HTML to look for HREF attributes of anchor tags and SRC attributes of IMG tags. Does anyone know of any libraries/freeware to help parse through...
6
by: Jocknerd | last post by:
I'm a Python newbie and I'm having trouble with Regular Expressions when reading in a text file. Here is a sample layout of the input file: 09/04/2004 Virginia 44 Temple ...
9
by: Holden Caulfield | last post by:
Hello! Does anyone know a good archive for all different types of Regular expressions? I am 90% done a website and the last 10% is putting validation on over 100 fields. They range from...
16
by: bissatch | last post by:
Hi, I am trying to use preg_replace to take out all occurances of PHP code after reading (fread()) the contents of a PHP file. The code I am using is: $html = preg_replace("<?php*?>", "",...
6
by: windandwaves | last post by:
Hi Folk Some of my clients asked me to create "fancy emails" for them (aka html formatted emails). I know how to make a nice html document, but I had trouble creating a simple way to provide...
3
by: Chris | last post by:
Hi everyone, I'm trying to parse through the contents of some text files with regular expressions, but am new to regular expressions and how to use them in VB.net. I'm pretty sure that the...
0
by: peridian | last post by:
Hi, I wanted a web page where I could post code to, and have it appear in coloured formatting based on the context of the code. Most of the techniques I have seen for this involve complex use...
47
by: Henning_Thornblad | last post by:
What can be the cause of the large difference between re.search and grep? This script takes about 5 min to run on my computer: #!/usr/bin/env python import re row="" for a in range(156000):...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.