[wp-trac] [WordPress Trac] #39791: sanitize_file_name() optimizations

WordPress Trac noreply at wordpress.org
Mon Feb 6 16:42:55 UTC 2017


#39791: sanitize_file_name() optimizations
-------------------------+------------------------------
 Reporter:  mgutt        |       Owner:
     Type:  enhancement  |      Status:  new
 Priority:  normal       |   Milestone:  Awaiting Review
Component:  Media        |     Version:  trunk
 Severity:  normal       |  Resolution:
 Keywords:               |     Focuses:
-------------------------+------------------------------

Comment (by mgutt):

 I benchmarked a str_replace() with the $special_chars. If it is realized
 it should use an preg_replace(). Its 3x times faster:

 {{{
 $filename = ltrim(preg_replace(
                 '~
                 [<>:"/\\|?*]|            # file system reserved
 https://en.wikipedia.org/wiki/Filename#Reserved_characters_and_words
                 [\x00-\x1F]|             # control characters
 http://msdn.microsoft.com/en-
 us/library/windows/desktop/aa365247%28v=vs.85%29.aspx
                 [\x7F\xA0\xAD]|          # non-printing characters DEL,
 NO-BREAK SPACE, SOFT HYPHEN
                 [#\[\]@!$&\'()+,;=]|     # URI reserved
 https://tools.ietf.org/html/rfc3986#section-2.2
                 [{}^\~`]                 # URL unsafe characters
 https://www.ietf.org/rfc/rfc1738.txt
                 ~x',
         // last step: ltrim() avoids ".", ".." or ".hiddenFiles"
         '-', $filename), '.-'));
 }
 }}}

--
Ticket URL: <https://core.trac.wordpress.org/ticket/39791#comment:2>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list