Matching a filename

This is a discussion on Matching a filename within the PHP Language forums, part of the PHP Programming Forums category; I wanna match the incorrect filename with the next regular expression but it doesnīt work. Why? I donīt ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-25-2005
Fran Garcia
 
Posts: n/a
Default Matching a filename

I wanna match the incorrect filename with the next regular expression
but it doesnīt work. Why? I donīt know.

if (ereg("[^A-Za-z0-9\.]+", $str))
return false;
else
return true;

I wanna allow only letters (A-Za-z) and numbers (0-9) and of course a
dot for the extension.

Have anybody any suggestion or idea? Any idea would be appreciated!!


Fran García
Reply With Quote
  #2 (permalink)  
Old 02-25-2005
Tim Van Wassenhove
 
Posts: n/a
Default Re: Matching a filename

On 2005-02-25, Fran Garcia <fgarcia@ua.es> wrote:
> I wanna match the incorrect filename with the next regular expression
> but it doesnīt work. Why? I donīt know.
>
> if (ereg("[^A-Za-z0-9\.]+", $str))
> return false;
> else
> return true;


return preg_match("#^[A-Za-z0-9\.]+$#", $str)

but what about $string = "C:\\My Documents" ?




--
Met vriendelijke groeten,
Tim Van Wassenhove <http://www.timvw.info>
Reply With Quote
  #3 (permalink)  
Old 02-26-2005
Chung Leong
 
Posts: n/a
Default Re: Matching a filename

"Tim Van Wassenhove" <timvw@users.sourceforge.net> wrote in message
news:3891bhF5kv67lU1@individual.net...
> On 2005-02-25, Fran Garcia <fgarcia@ua.es> wrote:
> > I wanna match the incorrect filename with the next regular expression
> > but it doesnīt work. Why? I donīt know.
> >
> > if (ereg("[^A-Za-z0-9\.]+", $str))
> > return false;
> > else
> > return true;

>
> return preg_match("#^[A-Za-z0-9\.]+$#", $str)
>
> but what about $string = "C:\\My Documents" ?


preg_match('/^[\w\.]+$/', $str) is more compact; covers the underscore as
well.


Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 10:54 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0