Re: Web friendly file names

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



(Sorry, I hit the wrong button and sent the reply only to Skip.)

On Wed, Jun 03, 2009 at 11:18:57AM -0500, Skip Evans wrote:

> Hey all,
>
> I have a file uploader module that allows users to upload
> documents and of course people are using all kinds of file
> names that are not web friendly.
>
> I guess the best solution is to replace any non alphanumeric
> with maybe '_' the underscore? How does that sound?
>
> Unfortunately, after 20+ years of coding I cannot get my brain
> around regular expressions to any decent level of proficiency,
> I know sad.
>
> I'd like to hear other solutions for this problem, I am
> thinking of a regexp that replaces special chars with the
> underscore; sounds pretty robust and globally acceptable?
>
> Opinions, witticisms, flames, quotations by famous débutantes?

Here's what I do on some of my webpages:

$fname = preg_replace("%[^A-Za-z0-9_\.]%", "_", $_POST['fname']);

This says to replace any character which isn't alphanumeric or
underscore or period, with an underscore. You can add characters to the
first expression in preg_replace to allow more characters, or change the
underscore expression with nothing ("") or whatever. I typically simply
eliminate bad stuff by using "" as the second parameter for
preg_replace.

To expand further on one thing: inside the first parameter, you'll see
the open square bracket ([) which indicates the beginning of a character
grouping or class. The next character, the caret (^) indicates negation.
So that expression means that anything which doesn't match these
characters gets whatever the next parameter is.

Paul

-- 
Paul M. Foster

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux