How Can I Convert Various Smart Quotes in PHP?

Susan Sarandon
Release: 2024-10-22 06:53:30
Original
391 people have browsed it

How Can I Convert Various Smart Quotes in PHP?

Converting Various Smart Quotes in PHP

Smart quotes are common variants of standard quotation marks that are utilized in typography to enhance the readability and aesthetic appeal of text. To ensure the correct conversion of these smart quotes into regular quotes when working with text, it's essential to consider the diverse range of smart quote characters used in different contexts.

Improved Function for Smart Quote Conversion:

The following enhanced function addresses the shortcomings of the original function you provided by incorporating a comprehensive mapping for various smart quote characters. It leverages both a character map ($chr_map) and pre-calculated arrays ($chr and $rpl) for optimal efficiency:

<code class="php">$chr_map = array(
    "\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

    "\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); // but: for efficiency you should
$rpl = array_values($chr_map); // pre-calculate these two arrays
$str = str_replace($chr, $rpl, html_entity_decode($str, ENT_QUOTES, "UTF-8"));</code>
Copy after login

This improved function utilizes a Unicode-compliant approach, covering a comprehensive range of smart quote characters. By leveraging pre-computed arrays, it maintains a high level of efficiency while ensuring accurate conversion of smart quotes to standard quotes.

Additional Considerations:

  • Encoding detection: If the input encoding is uncertain, use utf8_encode() to ensure UTF-8 encoding before applying the above function.
  • Normalizing Windows codepage 1252: For text that may originate from Windows codepage 1252, consider using a normalization map to convert specific code points to their Unicode equivalents.
  • Unicode character properties: Using character properties can further enhance smart quote handling, but their availability in regular expressions is limited.

The above is the detailed content of How Can I Convert Various Smart Quotes in PHP?. For more information, please follow other related articles on the PHP Chinese website!

source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!