>  기사  >  백엔드 개발  >  PHP에서 스마트 따옴표를 효과적으로 변환하는 방법: 종합 가이드

PHP에서 스마트 따옴표를 효과적으로 변환하는 방법: 종합 가이드

Mary-Kate Olsen
Mary-Kate Olsen원래의
2024-10-22 06:55:30920검색

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>

이 기능은 모든 유니코드 표준, Windows 코드 페이지 1252 및 둥근 따옴표에 대한 HTML 엔터티를 처리합니다. 모든 유형의 둥근 따옴표가 일반 따옴표로 정확하게 변환되도록 보장하여 PHP에서 따옴표 변환을 위한 포괄적인 솔루션을 제공합니다.

위 내용은 PHP에서 스마트 따옴표를 효과적으로 변환하는 방법: 종합 가이드의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.