使用PHP转换Microsoft Word特殊字符 [英] Converting Microsoft Word special characters with PHP
问题描述
我试图转换包含MS Word省略号和长破折号的用户粘贴的Word文本,然后再进行处理。
I am trying to convert Word text pasted by users that contain MS Word ellipsis and long dash before processing it further.
我发现了一个旧的建议解决方案问题 http://www.codingforums.com/archive/index.php/t-47163.html ,但它不适合我。例如,在替换省略号之后,变量返回为空。从来没有见过这样的事情:
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;
任何想法?
推荐答案
对于在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.
$search = [ // www.fileformat.info/info/unicode/<NUM>/ <NUM> = 2018
"\xC2\xAB", // « (U+00AB) in UTF-8
"\xC2\xBB", // » (U+00BB) in UTF-8
"\xE2\x80\x98", // ‘ (U+2018) in UTF-8
"\xE2\x80\x99", // ’ (U+2019) in UTF-8
"\xE2\x80\x9A", // ‚ (U+201A) in UTF-8
"\xE2\x80\x9B", // ‛ (U+201B) in UTF-8
"\xE2\x80\x9C", // " (U+201C) in UTF-8
"\xE2\x80\x9D", // " (U+201D) in UTF-8
"\xE2\x80\x9E", // „ (U+201E) in UTF-8
"\xE2\x80\x9F", // ‟ (U+201F) in UTF-8
"\xE2\x80\xB9", // ‹ (U+2039) in UTF-8
"\xE2\x80\xBA", // › (U+203A) in UTF-8
"\xE2\x80\x93", // – (U+2013) in UTF-8
"\xE2\x80\x94", // — (U+2014) in UTF-8
"\xE2\x80\xA6" // … (U+2026) in UTF-8
];
$replacements = [
"<<",
">>",
"'",
"'",
"'",
"'",
'"',
'"',
'"',
'"',
"<",
">",
"-",
"-",
"..."
];
str_replace($search, $replacements, $string);
这篇关于使用PHP转换Microsoft Word特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!