So konvertieren Sie intelligente Anführungszeichen effektiv in PHP: Eine umfassende Anleitung

Mary-Kate Olsen
Freigeben: 2024-10-22 06:55:30
Original
920 Leute haben es durchsucht

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

Intelligente Anführungszeichen in PHP konvertieren: Eine umfassende Anleitung

In PHP kann der Umgang mit intelligenten Anführungszeichen eine komplexe Aufgabe sein. Die folgende verbesserte Funktion gewährleistet eine umfassende Konvertierung aller Arten von intelligenten Anführungszeichen in reguläre Anführungszeichen:

<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>
Nach dem Login kopieren

Diese Funktion verarbeitet alle Unicode-Standards, die Windows-Codepage 1252 und HTML-Entitäten für intelligente Anführungszeichen. Es stellt sicher, dass alle Arten von intelligenten Angeboten genau in reguläre Angebote umgewandelt werden, und bietet eine umfassende Lösung für die Angebotskonvertierung in PHP.

Das obige ist der detaillierte Inhalt vonSo konvertieren Sie intelligente Anführungszeichen effektiv in PHP: Eine umfassende Anleitung. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Quelle:php
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Neueste Artikel des Autors
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!