如何在 PHP 中有效轉換智慧引號:綜合指南

Mary-Kate Olsen
發布: 2024-10-22 06:55:30
原創
923 人瀏覽過

How to Effectively Convert Smart Quotes in PHP: A Comprehensive Guide

在 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(
        "&amp;#8216;" => "'", // left single quotation mark
        "&amp;#8217;" => "'", // right single quotation mark
        "&amp;#8220;" => '"', // left double quotation mark
        "&amp;#8221;" => '"' // right double quotation mark
    );

    // Map Windows CP1252 entities
    $windows_cp1252 = array(
        "&amp;lsquo;" => "'", // left single quotation mark
        "&amp;rsquo;" => "'", // right single quotation mark
        "&amp;ldquo;" => '"', // left double quotation mark
        "&amp;rdquo;" => '"', // right double quotation mark
        "&amp;mdash;" => ' - ', // em dash
        "&amp;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中文網其他相關文章!

來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!