[Solved] Regex to remove invalid Windows filesystem chars
Moderator: General Moderators
- daedalus__
- DevNet Resident
- Posts: 1925
- Joined: Thu Feb 09, 2006 4:52 pm
[Solved] Regex to remove invalid Windows filesystem chars
I'm working on an error handling class, which I will be making a topic about later (look for it in theory).
I want to give the option to log errors, if none are fatal, to a file. In order to do this, I need to be able to remove characters that are invalid in the Windows filesystem from the path and filename of the log.
Here is my problem:
I am so bad with regular expressions that I can't even check for word characters without using \w (\W?).
I've been searching Google and the forums for a while but I can't turn anything up.
EDIT: I almost forgot, perl compatible, if you could. (preg_)
I want to give the option to log errors, if none are fatal, to a file. In order to do this, I need to be able to remove characters that are invalid in the Windows filesystem from the path and filename of the log.
Here is my problem:
I am so bad with regular expressions that I can't even check for word characters without using \w (\W?).
I've been searching Google and the forums for a while but I can't turn anything up.
EDIT: I almost forgot, perl compatible, if you could. (preg_)
- daedalus__
- DevNet Resident
- Posts: 1925
- Joined: Thu Feb 09, 2006 4:52 pm
omfsweetjesus
I got it!!!!!!!!!!!!!!!!!!!
Outputs:
!!!!!!!!!!!!!!!!!!!!!
"Replace any character that is not a through Z, a period, or an underscore."
I searched for almost 40 minutes before I tried to do it myself.
I got it!!!!!!!!!!!!!!!!!!!
Code: Select all
$string = 'this(is_an_invalid)file&name.lame';
echo $string.'<br />';
echo '<p>'.preg_replace('/[^a-zA-Z0-9._]/', '', $string).'</p>';Code: Select all
<p>thisis_an_invalidfilename.lame</p>"Replace any character that is not a through Z, a period, or an underscore."
I searched for almost 40 minutes before I tried to do it myself.
- daedalus__
- DevNet Resident
- Posts: 1925
- Joined: Thu Feb 09, 2006 4:52 pm
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Daedalus- wrote:[^a-zA-Z0-9._\/] preserves the fowardwack in path names as well.
Code: Select all
[^a-zA-Z0-9\._\/\-]Code: Select all
[^\w\.\/\-]- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
I guess you missed my reply to another of your recommendations to use \w before: viewtopic.php?p=245010#245010d11wtq wrote:Does it cover UTF-8 characters too or something? Like accented letters? This is new to me
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Yeah sorry I did. Interesting. Thanksfeyd wrote:I guess you missed my reply to another of your recommendations to use \w before: viewtopic.php?p=245010#245010d11wtq wrote:Does it cover UTF-8 characters too or something? Like accented letters? This is new to me
- daedalus__
- DevNet Resident
- Posts: 1925
- Joined: Thu Feb 09, 2006 4:52 pm