使用 PHP 转换 Microsoft Word 特殊字符
我正在尝试先转换用户粘贴的包含 MS Word 省略号和长破折号的 Word 文本,然后再进一步处理。
我在这里找到了一个旧的建议解决方案 http://www.codingforums .com/archive/index.php/t-47163.html ,但它对我不起作用。例如,替换省略号后,变量返回为空。以前从未见过这样的事情:
$src = "Long word dash – and weird Word ellipsis…";
$src = str_replace("‘", "'", $src);
$src = str_replace("’", "'", $src);
$src = str_replace("”", '"', $src);
$src = str_replace("“", '"', $src);
$src = str_replace("–", "-", $src);
$src = str_replace("…", "...", $src);
print $src;
有什么想法吗?
I am trying to convert Word text pasted by users that contain MS Word ellipsis and long dash before processing it further.
I found an old proposed solution here to the problem http://www.codingforums.com/archive/index.php/t-47163.html , but it does not work for me. After replacing the ellipsis for example , the variable comes back as empty. Never seen anything like this before:
$src = "Long word dash – and weird Word ellipsis…";
$src = str_replace("‘", "'", $src);
$src = str_replace("’", "'", $src);
$src = str_replace("”", '"', $src);
$src = str_replace("“", '"', $src);
$src = str_replace("–", "-", $src);
$src = str_replace("…", "...", $src);
print $src;
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对于在 PHP 中遇到菱形问号的人来说,这种替换 UTF-8 字符的方法比使用 chr 函数效果更好。
For anyone getting the diamond question mark in PHP, this method of replacing UTF-8 characters worked better than using the chr function.
唔。我使用此函数来清理复制到 RTE 中的文本。在这种情况下它可能起作用,也可能不起作用。它转换为 HTML 实体,但您可以调整它以仅转换为常规字符:
Hmm. I use this function for sanitizing text copied into an RTE. It may or may not work in this case. It converts to HTML entities, but you could tweak it to just convert to regular characters:
很好的解决方案。我复制并粘贴了它,并且没有出现任何问题。在进一步研究中,我添加了一些不在搜索和替换数组中的字符。为了找到 ASCII 字符 ID 号,我编写了一个 PHP 函数,它显示了 ASCII 字符号:
显示该字符,在其旁边的括号中显示 ascii 号。像这样:
echo stdump("GPUs…");
产生:
G(71)P(80)U(85)s(115)â(226)€(128)|(166)
希望这会有所帮助。
——基思
Great solution. I copied and pasted it and it worked with out a problem. On further study, I added a few characters that were not in the search and replace array. In order to find the ASCII character id numbers, I wrote a PHP function which shows what the ASCII character number is:
The character is display and next to it the ascii number is show in parenthesis. Like this:
echo stdump("GPUs…");
produces:
G(71)P(80)U(85)s(115)â(226)€(128)¦(166)
Hope this helps.
--Keith
它对我有用:
it works for me: