从上传的文件中删除特殊字符 [英] Remove Special Characters From Uploaded Files

查看:41
本文介绍了从上传的文件中删除特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了有关重音文件名的问题,并找到了清理文件名的功能,但我不确定这是否也会影响数据库中的文件名,还是只会重命名文件?

I am facing issues regarding the filenames that were accented and found a function to sanitize the filenames, but I am not sure of if this will affect the filenames in database as well or will just rename the files?

代码如下:

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

推荐答案

在函数文件中试试下面的代码.

Try below code in function file.

function sa_sanitize_spanish_chars($filename) {
$ext = end(explode('.',$filename));
$sanitized = preg_replace('/[^a-zA-Z0-9-_.]/','', substr($filename, 0, -(strlen($ext)+1)));
$sanitized = str_replace('.','-', $sanitized);
return strtolower($sanitized.'.'.$ext);
}

add_filter('sanitize_file_name', 'sa_sanitize_spanish_chars', 10);

这篇关于从上传的文件中删除特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆