http - How can I encode a filename in PHP according to RFC 2231? -


how can encode value of filename according encoding of mime parameter value , encoded word extensions: character sets, languages, , continuations (rfc 2231)?

i think should it:

function rfc2231_encode($name, $value, $charset='', $lang='', $ll=78) {     if (strlen($name) === 0 || preg_match('/[\x00-\x20*\'%()<>@,;:\\\\"\/[\]?=\x80-\xff]/', $name)) {         // invalid parameter name;         return false;     }     if (strlen($charset) !== 0 && !preg_match('/^[a-za-z]{1,8}(?:-[a-za-z]{1,8})*$/', $charset)) {         // invalid charset;         return false;     }     if (strlen($lang) !== 0 && !preg_match('/^[a-za-z]{1,8}(?:-[a-za-z]{1,8})*$/', $lang)) {         // invalid language;         return false;     }     $value = "$charset'$lang'".preg_replace_callback('/[\x00-\x20*\'%()<>@,;:\\\\"\/[\]?=\x80-\xff]/', function($match) { return rawurlencode($match[0]); }, $value);     $nlen = strlen($name);     $vlen = strlen($value);     if (strlen($name) + $vlen > $ll-3) {         $sections = array();         $section = 0;         ($i=0, $j=0; $i<$vlen; $i+=$j) {             $j = $ll - $nlen - strlen($section) - 4;             $sections[$section++] = substr($value, $i, $j);         }         ($i=0, $n=$section; $i<$n; $i++) {             $sections[$i] = " $name*$i*=".$sections[$i];         }         return implode(";\r\n", $sections);     } else {         return " $name*=$value";     } } 

note function expects output used in separate line preceded proper line wrap (i.e. crlf), e.g.:

"content-type: application/x-stuff;\r\n".rfc2231_encode('title', 'this more ***fun*** isn\'t it!', 'us-ascii', 'en', 48) 

the output is:

content-type: application/x-stuff;  title*0*=us-ascii'en'this%20is%20even%20more%20;  title*1=%2a%2a%2afun%2a%2a%2a%20isn%27t%20it! 

see test cases http content-disposition header field , encodings defined in rfc 2047 , rfc 2231/5987.


Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -