473,396 Members | 1,921 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,396 software developers and data experts.

preg_match - windows filenames

I am trying to work out the preg_match command to check windows
filenames for illegal characters and I can not seem to get it right.
The disallowed characters are \/:*?"<>|

I can not figure out the correct syntax.

Here's my best guess (which does not work). I'm using % to delimit the
characters and I've added backslashes to escape most of the characters
because they are also pattern modifiers.

$matches = preg_match("%\\/:\*\?\"<>\|%", $filename);
if ($matches > 0)
echo "illegal filename - $filename";

The above does not match the colon if I submit abc: as the filename.

Any help would be much appreciated (and maybe then I can figure out how
this works).

--
*****************************
Chuck Anderson • Boulder, CO
http://www.CycleTourist.com
Integrity is obvious.
The lack of it is common.
*****************************
Aug 2 '05 #1
3 4101
Chuck Anderson wrote:
The disallowed characters are \/:*?"<>|
[...]
%\\/:\*\?\"<>\|%


Set them in a character class, otherwise the pattern will
match only if *all* those characters occur _in the order you
specified_. Alternatives is another way, but less efficient.

%[\\\/:*?"<>|]%

(Note the three backslashes instead of two. The backslash
must be escaped twice - once because it's in a string and
again for the regex engine. The other characters don't need
escaping, except " if you double quote your string.)

--
Jock
Aug 2 '05 #2
John Dunlop wrote:
Chuck Anderson wrote:
The disallowed characters are \/:*?"<>|


[...]
%\\/:\*\?\"<>\|%


Set them in a character class, otherwise the pattern will
match only if *all* those characters occur _in the order you
specified_. Alternatives is another way, but less efficient.

%[\\\/:*?"<>|]%

(Note the three backslashes instead of two. The backslash
must be escaped twice - once because it's in a string and
again for the regex engine. The other characters don't need
escaping, except " if you double quote your string.)

Thank you, thank you. I hesitated asking because it seemed like I was
looking for a "free handout."
This clarifies things, though, and now I know more about regular
expressions.

--
*****************************
Chuck Anderson • Boulder, CO
http://www.CycleTourist.com
Integrity is obvious.
The lack of it is common.
*****************************
Aug 2 '05 #3
*** Chuck Anderson wrote/escribió (Tue, 02 Aug 2005 12:43:27 -0600):
I am trying to work out the preg_match command to check windows
filenames for illegal characters and I can not seem to get it right.
The disallowed characters are \/:*?"<>|

I can not figure out the correct syntax.


Hope this helps (not tested):

$forbidden=preg_quote('\/:*?"<>', '/');
preg_match('/[' . $forbidden . ']/', $filename);
--
-- Álvaro G. Vicario - Burgos, Spain
-- http://bits.demogracia.com - Mi sitio sobre programación web
-- Don't e-mail me your questions, post them to the group
--
Aug 3 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: fartsniff | last post by:
hello all, here is a preg_match routine that i am using. basically, $image is set in some code above, and it can be either st-1.gif or sb-1.gif (actually it randomly picks them from about 100...
0
by: DaveF | last post by:
It seems to start downloading files as the service is tryig to start using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Diagnostics; using...
22
by: rtilley | last post by:
# Spaces are present before and after the XXX filename = ' XXX ' new_filename = filename.strip() if new_filename != filename: print filename Macs allow these spaces in file and folder...
5
by: Mark Woodward | last post by:
Hi all, I'm trying to validate text in a HTML input field. How do I *allow* a single quote? // catch any nasty characters (eg !@#$%^&*()/\) $match = '/^+$/'; $valid_srch = preg_match($match,...
6
by: mantrid | last post by:
Hello Found this piece of code using preg_match to check file types during upload of files. $allowed_file_types = "(jpg|jpeg|gif|bmp|png)"; preg_match("/\." . $allowed_file_types . "$/i",...
8
by: k.i.n.g. | last post by:
Hi ALL, I am a newbee programmer and started of with python recently. I am trying write a script which backups outlook (.pst ) files everytime I shutdown my system. I have writtern following...
3
by: Torsten Bronger | last post by:
Hallöchen! I'd like to map general unicode strings to safe filename. I tried punycode but it is case-sensitive, which Windows is not. Thus, "Hallo" and "hallo" are mapped to "Hallo-" and...
2
by: Art | last post by:
hey, i think this must be really easy: i want to use strings as filenames that include characters that are not allowed in windows filenames. is there a special converter method for this?...
23
by: Stewart Berman | last post by:
I am trying to develop a wrapper class for the Windows API functions in Visual Studio 2008: GetOpenFileName GetSaveFileName I put together a starter class: using System; using...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.