去掉文件名中的粗体字符

huangapple go评论73阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年6月1日 20:51:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76382065.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定