通过 PHP 替换 Word 错误

发布于 2024-08-30 20:42:30 字数 460 浏览 1 评论 0原文

内容人员一直在使用 Word 并将内容粘贴到旧的 unicode 系统中。我现在正在尝试使用UTF8。

但是,导入数据后,有些字符我无法删除。

我已尝试以下 stackoverflow 线程,但提供的函数均无法修复此字符串: http:// /snipplr.com/view.php?codeview&id=11171 / 如何在 PHP 中替换 Microsoft 编码的引号

字符串:Dan’s back for more!!

Content people have been using Word and pasting things into the old unicode system. I'm now trying to go UTF8.

However, upon importing the data there are characters I cannot get rid of.

I have tried the following stackoverflow thread and none of the functions provided fix this string: http://snipplr.com/view.php?codeview&id=11171 / How to replace Microsoft-encoded quotes in PHP

String: Danâ??s back for more!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沒落の蓅哖 2024-09-06 20:42:30

在这种情况下,我通常从从单词复制粘贴的字符串开始:

$str = 'Danâ’s back !';
var_dump($str);

并且,我逐字节输出每个字节的十六进制代码:

for ($i=0 ; $i<strlen($str) ; $i++) {
    $byte = $str[$i];
    $char = ord($byte);
    printf('%s:0x%02x ', $byte, $char);
}

这给出了如下输出:

D:0x44 a:0x61 n:0x6e �:0xc3 �:0xa2 �:0xe2 �:0x80 �:0x99 s:0x73 :0x20 b:0x62 a:0x61 c:0x63 k:0x6b :0x20 !:0x21 

然后,通过一些猜测、运气和反复试验,您会发现:

  • â 是一个适合两个字节的字符:0xc3 0xa2
  • 特殊引号是一个适合三个字节的字符:0xe2 0x80 0x99

提示:当您没有两个特殊字符相互跟随时,会更容易;-)

之后,只需使用 str_replace 将正确的字节序列替换为另一个字符即可;例如,用普通引号替换特殊引号:

var_dump(str_replace("\xe2\x80\x99", "'", $str));

将为您提供:

string 'Danâ's back !' (length=14)

In this kind of situation, I generally start with the string I have copy-pasted from word :

$str = 'Danâ’s back !';
var_dump($str);

And, going byte-by-byte in it, I output the hexadecimal code of each byte :

for ($i=0 ; $i<strlen($str) ; $i++) {
    $byte = $str[$i];
    $char = ord($byte);
    printf('%s:0x%02x ', $byte, $char);
}

Which gives an output such as this one :

D:0x44 a:0x61 n:0x6e �:0xc3 �:0xa2 �:0xe2 �:0x80 �:0x99 s:0x73 :0x20 b:0x62 a:0x61 c:0x63 k:0x6b :0x20 !:0x21 

Then, with a bit of guessing, luck, and trial-and-error, you'll find out that :

  • â is a character that fits on two bytes : 0xc3 0xa2
  • and the special-quote is a character that fits on three bytes : 0xe2 0x80 0x99

Hint : it's easier when you don't have two special characters following each other ;-)

After that, it's only a matter of using str_replace to replace the correct sequence of bytes by another character ; for example, to replace the special-quote by a normal one :

var_dump(str_replace("\xe2\x80\x99", "'", $str));

Will give you :

string 'Danâ's back !' (length=14)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文