在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中文網其他相關文章!