替换图像上传Wordpress上的非ASCII字符

时间:2017-01-31 14:16:11

标签: php wordpress

我尝试在functions.php中包含以下2个片段

add_filter('sanitize_file_name', 'remove_accents' ); 

add_filter('sanitize_file_name', 'sa_sanitize_spanish_chars', 10);
function sa_sanitize_spanish_chars ($filename) {
return remove_accents( $filename );
}

上传图片时,应该用“e”等标准ASCII字符替换“é”等法语字符。但它不起作用。

我尝试在2个不同的WP安装(3.8和4.6)中插入代码,但无济于事。

替换字符的全部需要是因为Safari无法读取其中包含非ASCII字符的网址。

1 个答案:

答案 0 :(得分:0)

MickaëlGris的解决方案是以下代码,它也适用于4.6和3.8,只是为了确保:

function wpc_sanitize_french_chars($ filename){

/* Force the file name in UTF-8 (encoding Windows / OS X / Linux) */
$filemane = mb_convert_encoding($filename, "UTF-8");

$char_not_clean = array('/À/','/Á/','/Â/','/Ã/','/Ä/','/Å/','/Ç/','/È/','/É/','/Ê/','/Ë/','/Ì/','/Í/','/Î/','/Ï/','/Ò/','/Ó/','/Ô/','/Õ/','/Ö/','/Ù/','/Ú/','/Û/','/Ü/','/Ý/','/à/','/á/','/â/','/ã/','/ä/','/å/','/ç/','/è/','/é/','/ê/','/ë/','/ì/','/í/','/î/','/ï/','/ð/','/ò/','/ó/','/ô/','/õ/','/ö/','/ù/','/ú/','/û/','/ü/','/ý/','/ÿ/', '/©/');
$clean = array('a','a','a','a','a','a','c','e','e','e','e','i','i','i','i','o','o','o','o','o','u','u','u','u','y','a','a','a','a','a','a','c','e','e','e','e','i','i','i','i','o','o','o','o','o','o','u','u','u','u','y','y','copy');

$friendly_filename = preg_replace($char_not_clean, $clean, $filename);


/* After replacement, we destroy the last residues */
$friendly_filename = utf8_decode($friendly_filename);
$friendly_filename = preg_replace('/\?/', '', $friendly_filename);


/* Lowercase */
$friendly_filename = strtolower($friendly_filename);

return $friendly_filename;

} add_filter('sanitize_file_name','wpc_sanitize_french_chars',10);