使用PHP转换所有类型的智能报价

Xeoncross

我正在尝试使用文本时将所有类型的智能引号转换为常规引号。但是,我编译的以下函数似乎仍然缺乏支持和适当的设计。

有人知道如何正确转换所有引号字符吗?

function convert_smart_quotes($string)
{
    $quotes = array(
        "\xC2\xAB"   => '"', // « (U+00AB) in UTF-8
        "\xC2\xBB"   => '"', // » (U+00BB) in UTF-8
        "\xE2\x80\x98" => "'", // ‘ (U+2018) in UTF-8
        "\xE2\x80\x99" => "'", // ’ (U+2019) in UTF-8
        "\xE2\x80\x9A" => "'", // ‚ (U+201A) in UTF-8
        "\xE2\x80\x9B" => "'", // ‛ (U+201B) in UTF-8
        "\xE2\x80\x9C" => '"', // “ (U+201C) in UTF-8
        "\xE2\x80\x9D" => '"', // ” (U+201D) in UTF-8
        "\xE2\x80\x9E" => '"', // „ (U+201E) in UTF-8
        "\xE2\x80\x9F" => '"', // ‟ (U+201F) in UTF-8
        "\xE2\x80\xB9" => "'", // ‹ (U+2039) in UTF-8
        "\xE2\x80\xBA" => "'", // › (U+203A) in UTF-8
    );
    $string = strtr($string, $quotes);

    // Version 2
    $search = array(
        chr(145),
        chr(146),
        chr(147),
        chr(148),
        chr(151)
    );
    $replace = array("'","'",'"','"',' - ');
    $string = str_replace($search, $replace, $string);

    // Version 3
    $string = str_replace(
        array('‘','’','“','”'),
        array("'", "'", '"', '"'),
        $string
    );

    // Version 4
    $search = array(
        '‘', 
        '’', 
        '“', 
        '”', 
        '—',
        '–',
    );
    $replace = array("'","'",'"','"',' - ', '-');
    $string = str_replace($search, $replace, $string);

    return $string;
}

注意:此问题是有关全部报价(包括此处要求“ Microsoft”引号)的完整查询。这是“重复”,与询问所有轮胎尺寸与要求汽车轮胎的“重复”相同尺寸。

沃尔特·特罗斯

您需要类似以下内容(假设输入UTF-8,而忽略CJK(中文,日文,韩文)):

$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     // U+0022 quotation mark (")
                          // U+0027 apostrophe     (')
   "\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"));

这里是背景:

每个Unicode字符都完全属于一个“常规类别”,其中可以包含引号字符的是以下类别

(这些页面可方便地检查您是否没有错过任何内容-还有类别索引

在启用Unicode的正则表达式中匹配这些类别有时会很有用

此外,Unicode字符具有“属性”,您对此感兴趣的是Quotation_Mark不幸的是,这些不能在正则表达式中访问。

在Wikipedia中,您可以找到具有Quotation_Mark属性的字符组最终参考是unicode.org上的PropList.txt,但这是一个ASCII文本文件。

如果您还需要翻译CJK字符,则只需获取它们的代码点,确定其翻译并找到其UTF-8编码,例如,通过在fileformat.info中查找它(例如,对于U + 301E:http ://www.fileformat.info/info/unicode/char/301e/index.htm)。

关于Windows代码页1252:Unicode定义了前256个代码点,以表示与ISO-8859-1完全相同的字符,但是ISO-8859-1通常与Windows代码页1252混淆,因此所有浏览器都呈现0x80-0x9F范围,在ISO-8859-1中为“空”(更准确地说:它包含控制字符),就像Windows代码页1252一样。Wikipedia页中的表列出了Unicode等效项。

注意:strtr()通常比慢str_replace()使用您的输入和PHP版本为其计时。如果速度足够快,您可以直接使用地图,例如my $chr_map


如果不确定您的输入是UTF-8编码的,并且愿意假设输入不是UTF-8编码的,那么它是ISO-8859-1或Windows代码页1252,那么您可以在执行其他操作之前执行此操作:

if ( !preg_match('/^\\X*$/u', $str)) {
   $str = utf8_encode($str);
}

警告:尽管在极少数情况下,此正则表达式可能无法检测到非UTF-8编码。例如:"Gruß…"/*CP-1252*/=="Gru\xDF\x85"看起来像此正则表达式的UTF-8(U + 07C5是N'ko数字5)。该正则表达式可以稍作增强,但是不幸的是,可以证明,对于编码检测问题,不存在完全可靠的解决方案。


如果要将Windows代码页1252产生的范围0x80-0x9F标准化为常规Unicode代码点,则可以执行此操作(并删除$chr_map上面的第一部分):

$normalization_map = array(
   "\xC2\x80" => "\xE2\x82\xAC", // U+20AC Euro sign
   "\xC2\x82" => "\xE2\x80\x9A", // U+201A single low-9 quotation mark
   "\xC2\x83" => "\xC6\x92",     // U+0192 latin small letter f with hook
   "\xC2\x84" => "\xE2\x80\x9E", // U+201E double low-9 quotation mark
   "\xC2\x85" => "\xE2\x80\xA6", // U+2026 horizontal ellipsis
   "\xC2\x86" => "\xE2\x80\xA0", // U+2020 dagger
   "\xC2\x87" => "\xE2\x80\xA1", // U+2021 double dagger
   "\xC2\x88" => "\xCB\x86",     // U+02C6 modifier letter circumflex accent
   "\xC2\x89" => "\xE2\x80\xB0", // U+2030 per mille sign
   "\xC2\x8A" => "\xC5\xA0",     // U+0160 latin capital letter s with caron
   "\xC2\x8B" => "\xE2\x80\xB9", // U+2039 single left-pointing angle quotation mark
   "\xC2\x8C" => "\xC5\x92",     // U+0152 latin capital ligature oe
   "\xC2\x8E" => "\xC5\xBD",     // U+017D latin capital letter z with caron
   "\xC2\x91" => "\xE2\x80\x98", // U+2018 left single quotation mark
   "\xC2\x92" => "\xE2\x80\x99", // U+2019 right single quotation mark
   "\xC2\x93" => "\xE2\x80\x9C", // U+201C left double quotation mark
   "\xC2\x94" => "\xE2\x80\x9D", // U+201D right double quotation mark
   "\xC2\x95" => "\xE2\x80\xA2", // U+2022 bullet
   "\xC2\x96" => "\xE2\x80\x93", // U+2013 en dash
   "\xC2\x97" => "\xE2\x80\x94", // U+2014 em dash
   "\xC2\x98" => "\xCB\x9C",     // U+02DC small tilde
   "\xC2\x99" => "\xE2\x84\xA2", // U+2122 trade mark sign
   "\xC2\x9A" => "\xC5\xA1",     // U+0161 latin small letter s with caron
   "\xC2\x9B" => "\xE2\x80\xBA", // U+203A single right-pointing angle quotation mark
   "\xC2\x9C" => "\xC5\x93",     // U+0153 latin small ligature oe
   "\xC2\x9E" => "\xC5\xBE",     // U+017E latin small letter z with caron
   "\xC2\x9F" => "\xC5\xB8",     // U+0178 latin capital letter y with diaeresis
);
$chr = array_keys  ($normalization_map); // but: for efficiency you should
$rpl = array_values($normalization_map); // pre-calculate these two arrays
$str = str_replace($chr, $rpl, $str);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

替换美丽汤中的所有智能报价

为什么在使用'?'时Rust会在错误类型之间隐式转换?但没有回报价值?

回声但保留所有报价

python中的智能类型转换

将所有英文报价替换为德国报价

PHP的cookie有报价删除

如何使用 From<T> 制作可从所有其他类型转换的类型?

转换联合类型的所有元素

PHP将所有整数转换为1种类型的值

如何在 Binance API 中使用通配符返回特定基础资产的所有报价资产

用常规报价替换智能报价

递归转换嵌套对象类型以修改所有值类型

使用dos2unix转换所有子目录中的某些文件类型

C ++ 11智能指针的所有权和强制转换

Kotlin修饰类型参数无法智能转换

JOLT:转换所有“数组”类型的 json 元素

在 Julia 中转换特定类型的所有 DataFrame 列

扩展所有 Iterators<Item = Result<Type, E>> 以转换类型

如何禁用基本类型的所有隐式转换?

选择列,但强制转换给定类型的所有列

打字稿:通用函数类型转换所有函数参数

使用UIActivityViewController共享所有类型的文件

C ++,如何使用所有类型的子集

允许方法使用所有继承的类型

列出用户使用的所有外壳类型

preg匹配,用于使用php从页面获取所有类型的电话号码

Hybris使用CmsItemConverter转换所有属性

将CSV Serde与Hive创建表结合使用可将所有字段类型转换为字符串

类型错误:在使用 MySQL 进行字符串格式化期间并非所有参数都被转换