在 PHP 中转换智能引号:详尽的解决方案
智能引号是用于指示文本中的直接语音或引文的印刷标记。它们可以增强可读性并增加书面内容的细微差别。然而,在 PHP 等编程语言中使用智能引号需要进行适当的处理,以确保准确的显示和转换。
问题陈述
提供的 PHP 函数旨在转换各种类型智能引号与常规(直接)引号的区别。然而,它缺乏处理所有报价变化的全面支持。挑战在于识别所有可能代表智能引号的 Unicode 字符并实现强大的转换机制。
解决方案
为了有效地转换所有类型的智能引号,我们需要在不同的 Unicode 字符与其相应的常规引号对应项之间创建全面的映射。以下代码片段提供了增强的解决方案:
<code class="php">$chr_map = array( // Windows codepage 1252 "\xC2\x82" => "'", // U+0082⇒U+201A single low-9 quotation mark "\xC2\x84" => '"', // U+0084⇒U+201E double low-9 quotation mark "\xC2\x8B" => "'", // U+008B⇒U+2039 single left-pointing angle quotation mark "\xC2\x91" => "'", // U+0091⇒U+2018 left single quotation mark "\xC2\x92" => "'", // U+0092⇒U+2019 right single quotation mark "\xC2\x93" => '"', // U+0093⇒U+201C left double quotation mark "\xC2\x94" => '"', // U+0094⇒U+201D right double quotation mark "\xC2\x9B" => "'", // U+009B⇒U+203A single right-pointing angle quotation mark // Regular Unicode "\xC2\xAB" => '"', // U+00AB left-pointing double angle quotation mark "\xC2\xBB" => '"', // U+00BB right-pointing double angle quotation mark "\xE2\x80\x98" => "'", // U+2018 left single quotation mark "\xE2\x80\x99" => "'", // U+2019 right single quotation mark "\xE2\x80\x9A" => "'", // U+201A single low-9 quotation mark "\xE2\x80\x9B" => "'", // U+201B single high-reversed-9 quotation mark "\xE2\x80\x9C" => '"', // U+201C left double quotation mark "\xE2\x80\x9D" => '"', // U+201D right double quotation mark "\xE2\x80\x9E" => '"', // U+201E double low-9 quotation mark "\xE2\x80\x9F" => '"', // U+201F double high-reversed-9 quotation mark "\xE2\x80\xB9" => "'", // U+2039 single left-pointing angle quotation mark "\xE2\x80\xBA" => "'", // U+203A single right-pointing angle quotation mark ); $chr = array_keys ($chr_map); $rpl = array_values($chr_map); $str = str_replace($chr, $rpl, html_entity_decode($str, ENT_QUOTES, "UTF-8"));</code>
此增强版本同时考虑 Windows 代码页 1252 和常规 Unicode 字符,确保全面转换。
其他注意事项
通过遵循这些准则,您可以在 PHP 中实现强大而全面的智能报价转换机制,确保准确处理各种报价变化。
以上是如何在 PHP 中有效地将智能引号转换为常规引号?的详细内容。更多信息请关注PHP中文网其他相关文章!