Function for replacing characters that can't be used in a filename 
Author Message
 Function for replacing characters that can't be used in a filename

Is there a function in PHP for replacing characters that can't be used in a filename? This
is eg. '*', '?', '/', '\', etc...
Just remove them or replace them with '_' would be fine...

--

With kindest regards,
Met vriendelijke groeten,

Spike - http://www.*-*-*.com/



Sun, 06 Feb 2005 09:08:41 GMT  
 Function for replacing characters that can't be used in a filename
Okay, I used this, but is this enough?

$filename = str_replace(
array (  ':',    '/',    '?',    '*',    '<',    '>',    '|',    '"',    '\\'    ),
array ( '_',    '_',    '_',    '_',    '_',    '_',    '_',    '_',    '_'   ),
$filename
);

Is there a function built in PHP already? I couldn't find one...

--

With kindest regards,
Met vriendelijke groeten,

Spike - http://www.wazzoo.net



Quote:
> Is there a function in PHP for replacing characters that can't be used in a filename?
This
> is eg. '*', '?', '/', '\', etc...
> Just remove them or replace them with '_' would be fine...

> --

> With kindest regards,
> Met vriendelijke groeten,

> Spike - http://www.wazzoo.net



Sun, 06 Feb 2005 09:24:58 GMT  
 Function for replacing characters that can't be used in a filename

Quote:

> Is there a function in PHP for replacing characters that can't be used
> in a filename? This is eg. '*', '?', '/', '\', etc...  Just remove
> them or replace them with '_' would be fine...

str_replace or [ep]reg_replace or maybe url_encode if you want to
"preserve" the characters.

BTW real filesystem have no problems with the characters you mention
(except the directory delimiter (often '/' :)).

--

  Daniel Tryba



Sun, 06 Feb 2005 09:42:25 GMT  
 Function for replacing characters that can't be used in a filename
There isn't a function that does that that I am aware of. I might do:

preg_replace( "/(*|?|\/|\\)/", "_", $string);

Things to note:

    - Seperate the chars to look for with a pike '|'
    - Any forward slash must be escaped aside from the first and last.
    - Backslash is escaped as normal

-Joe


Quote:
> Is there a function in PHP for replacing characters that can't be used in
a filename? This
> is eg. '*', '?', '/', '\', etc...
> Just remove them or replace them with '_' would be fine...

> --

> With kindest regards,
> Met vriendelijke groeten,

> Spike - http://www.wazzoo.net



Sun, 06 Feb 2005 10:21:27 GMT  
 Function for replacing characters that can't be used in a filename

Quote:
> $filename = str_replace(
> array (  ':',    '/',    '?',    '*',    '<',    '>',    '|',    '"',
'\\'    ),
> array ( '_',    '_',    '_',    '_',    '_',    '_',    '_',    '_',
'_'   ),
> $filename
> );

that could just be:

$filename = str_replace(
array (  ':',    '/',    '?',    '*',    '<',    '>',    '|',    '"',
'\\'    ),
'_',
$filename
);

Couldn't it? Then all instances of search are replaced with an underscore.

And you could probably put the search array outside of the str_replace too,
to make it more maintainable/editable...

--

Regards,
Christopher Pole
ng at rocketfish dot net



Sun, 06 Feb 2005 11:29:30 GMT  
 Function for replacing characters that can't be used in a filename

Quote:
> Is there a function in PHP for replacing characters that can't be used in a filename? This
> is eg. '*', '?', '/', '\', etc...
> Just remove them or replace them with '_' would be fine...

Why don't you urlencode() the filename? Then it's easy to translate it
back as well.

Martin

--

  "The website you seek can not be located, but countless more exist."



Sun, 06 Feb 2005 18:22:29 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. string.replace() can't replace newline characters???

2. Using CGI module with 'canned queries'

3. Python function to replace special characters in HTML?

4. Python function to replace special characters in HTML?

5. How do I replace a file using Write Characters to file.vi

6. Issues with Non-COBOL characters and REPLACE / REPLACING

7. Using string map to replace Special Characters (MS Word)

8. Using variable filename(!FILENAME)

9. How to replace or create a file using the open/create/replace.vi

10. Window's 2000 Global Find and Replace Solution using awk

11. It's not bad canned meat...

12. It's not bad canned meat...

 

 
Powered by phpBB® Forum Software