英文:
getting rid of bold characters in a filename
问题
mysql最近向我报告了以下错误:[HY000][1366] 列 'name' 的值错误:'\xF0\x9D\x98\xBD\xF0\x9D...'
经过调查,我发现奇怪字符的值来自文件名,显然包含粗体字符:4 𝘽𝘼𝙉𝘿𝙀 𝘼𝙉𝙉𝙊𝙉𝘾𝙀 - TV.mp4
与其更改数据库的编码以接受这些字符,我宁愿在将其插入数据库之前在PHP中对值进行清理。但我不知道应该执行哪个操作才能得到以下经过清理的值:4 BANDE ANNONCE - TV.mp4
任何帮助将不胜感激。
英文:
mysql recently reported me the following error: [HY000][1366] Incorrect string value: '\xF0\x9D\x98\xBD\xF0\x9D...' for column 'name'
after investigation, I found that the value with weird characters comes from a filename, which apparently contains bold characters: 4 𝘽𝘼𝙉𝘿𝙀 𝘼𝙉𝙉𝙊𝙉𝘾𝙀 - TV.mp4
Instead of changing the encoding of my database to accept such characters, i'd rather sanitize the value before inserting it, in PHP. But I have no idea which operation I should run to end with the following sanitized value : 4 BANDE ANNONCE - TV.mp4
Any help would be appreciated.
答案1
得分: 2
你可以使用PHP的iconv
函数将字符串从一种字符编码转换为另一种。在这种情况下,你可以尝试将字符串从UTF-8
转换为ASCII//TRANSLIT
,这将尝试将任何非ASCII字符转换为它们最接近的ASCII等效字符。
以下是一个示例:
function sanitize_string($input_string) {
$sanitized_string = iconv("UTF-8", "ASCII//TRANSLIT", $input_string);
return $sanitized_string;
}
$filename = "4 𝘽𝘼𝙉𝘿𝙀 𝘼𝙉𝙉𝙊𝙉𝘾𝙀 - TV.mp4";
$sanitized_filename = sanitize_string($filename);
echo $sanitized_filename;
这应该输出4 BANDE ANNONCE - TV.mp4
,这是你要查找的经过处理的值。
英文:
You can use the PHP iconv
function to convert the string from one character encoding to another. In this case, you can try converting the string from UTF-8
to ASCII//TRANSLIT
, which will attempt to transliterate any non-ASCII characters into their closest ASCII equivalents.
Here's an example:
function sanitize_string($input_string) {
$sanitized_string = iconv("UTF-8", "ASCII//TRANSLIT", $input_string);
return $sanitized_string;
}
$filename = "4 𝘽𝘼𝙉𝘿𝙀 𝘼𝙉𝙉𝙊𝙉𝘾𝙀 - TV.mp4";
$sanitized_filename = sanitize_string($filename);
echo $sanitized_filename;
This should output 4 BANDE ANNONCE - TV.mp4
, which is the sanitized value you're looking for.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论