Jump to content

charcters in UNIX file and directory names


mridang_agarwal

Recommended Posts

UNIX filenames or Directory name can contain anything other than a / Character.

I dont have Windows I use Linux

In windows

Create a new file and then try renaminf it to a String that contains a / when you press that Windows will show you which characters are not Allowed

i have string. how do i check if it only contains numbers, aplhabtes, . - and _

 

Use preg_match.

 

UNIX filenames or Directory name can contain anything other than a / Character.

 

But I wouldn't recommend using anything other than what Ive suggested. Special chars can wreak havoc with shell expansion.

I think you should take thorpe's advice and only allow letters, numbers, underscore, minus and dot.

 

If that is the case a regex to test a filename would look like this:

 

preg_match("/([a-z0-9\._-])+/i",$filename)

 

 

And for the path:

preg_match("/([a-z0-9\.\\\/_-])+/",$path)

 

I'm not 100% sure if / (slash) is a special character and needs escaping by \ (backslash). So the "\\\/" part of the regex is first escaping a backslash "\\" and then escaping a slash "\/" but notice that I wasn't sure if / (slash) needed escaping. Furthermore I don't know if you want both / and \ to be allowed in the path...

 

And if the two code pieces don't speak for them selves I would recommend you to split up the path in "path" and "filename" :)

 

 

By the way this is a very useful regex site: http://www.regular-expressions.info/reference.html

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.