在 PHP 中转换智能引号:综合指南
在 PHP 中,处理智能引号可能是一项复杂的任务。以下改进的函数可确保将所有类型的智能引号全面转换为常规引号:
<code class="php">function convert_smart_quotes($string) { $unicode_map = array( "\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 // 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 ); // Map special HTML entities $html_entities = array( "&#8216;" => "'", // left single quotation mark "&#8217;" => "'", // right single quotation mark "&#8220;" => '"', // left double quotation mark "&#8221;" => '"' // right double quotation mark ); // Map Windows CP1252 entities $windows_cp1252 = array( "&lsquo;" => "'", // left single quotation mark "&rsquo;" => "'", // right single quotation mark "&ldquo;" => '"', // left double quotation mark "&rdquo;" => '"', // right double quotation mark "&mdash;" => ' - ', // em dash "&ndash;" => '- ' // en dash ); // Unicode first $string = str_replace( array_keys ($unicode_map), array_values($unicode_map), html_entity_decode($string, ENT_QUOTES, "UTF-8") ); // Windows CP1252 next $string = str_replace( array_keys ($windows_cp1252), array_values($windows_cp1252), $string ); // Finally, HTML entities $string = str_replace( array_keys ($html_entities), array_values($html_entities), $string ); return $string; }</code>
此函数处理智能引号的所有 Unicode 标准、Windows 代码页 1252 和 HTML 实体。它确保所有类型的智能报价都能准确转换为常规报价,为PHP中的报价转换提供全面的解决方案。
以上是如何在 PHP 中有效转换智能引号:综合指南的详细内容。更多信息请关注PHP中文网其他相关文章!