栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

您需要这样的内容(假设输入UTF-8,而忽略CJK(中文,日文,韩文)):

$chr_map = array(   // Windows prepage 1252   "xC2x82" => "'", // U+0082⇒U+201A single low-9 quotation mark   "xC2x84" => '"', // U+0084⇒U+201E double low-9 quotation mark   "xC2x8B" => "'", // U+008B⇒U+2039 single left-pointing angle quotation mark   "xC2x91" => "'", // U+0091⇒U+2018 left single quotation mark   "xC2x92" => "'", // U+0092⇒U+2019 right single quotation mark   "xC2x93" => '"', // U+0093⇒U+201C left double quotation mark   "xC2x94" => '"', // U+0094⇒U+201D right double quotation mark   "xC2x9B" => "'", // U+009B⇒U+203A single right-pointing angle quotation mark   // Regular Unipre     // U+0022 quotation mark (")    // U+0027 apostrophe     (')   "xC2xAB"     => '"', // U+00AB left-pointing double angle quotation mark   "xC2xBB"     => '"', // U+00BB right-pointing double angle quotation mark   "xE2x80x98" => "'", // U+2018 left single quotation mark   "xE2x80x99" => "'", // U+2019 right single quotation mark   "xE2x80x9A" => "'", // U+201A single low-9 quotation mark   "xE2x80x9B" => "'", // U+201B single high-reversed-9 quotation mark   "xE2x80x9C" => '"', // U+201C left double quotation mark   "xE2x80x9D" => '"', // U+201D right double quotation mark   "xE2x80x9E" => '"', // U+201E double low-9 quotation mark   "xE2x80x9F" => '"', // U+201F double high-reversed-9 quotation mark   "xE2x80xB9" => "'", // U+2039 single left-pointing angle quotation mark   "xE2x80xBA" => "'", // 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_depre($str, ENT_QUOTES, "UTF-8"));

这里是背景:

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

  • Ps
    “标点符号,打开”
  • Pe
    “标点符号,关闭”
  • Pi
    “标点符号,初始引号(根据用法,其行为可能类似于Ps或Pe)”
  • Pf
    “标点符号,最终报价(根据用法,其行为可能类似于Ps或Pe)”
  • Po
    “标点符号,其他”

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

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

此外,Unipre字符具有“属性”,您感兴趣的是

Quotation_Mark
。不幸的是,这些不能在正则表达式中访问。

在Wikipedia中,您可以找到具有

Quotation_Mark
属性的字符组。最终参考是unipre.org上的PropList.txt,但这是一个ASCII文本文件。

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

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

注意:

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


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

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

警告:不过,这种正则表达式在极少数情况下可能无法检测到非UTF-8编码。例如:

"Gruß…"=="GruxDFx85"
看起来像此正则表达式的UTF-8(U
+ 07C5是N’ko数字5)。该正则表达式可以稍作增强,但是不幸的是,可以证明,对于编码检测问题,不存在完全可靠的解决方案。


如果要将Windows代码页1252产生的范围0x80-0x9F标准化为常规Unipre代码点,则可以执行此操作(并删除

$chr_map
上面的第一部分):

$normalization_map = array(   "xC2x80" => "xE2x82xAC", // U+20AC Euro sign   "xC2x82" => "xE2x80x9A", // U+201A single low-9 quotation mark   "xC2x83" => "xC6x92",     // U+0192 latin small letter f with hook   "xC2x84" => "xE2x80x9E", // U+201E double low-9 quotation mark   "xC2x85" => "xE2x80xA6", // U+2026 horizontal ellipsis   "xC2x86" => "xE2x80xA0", // U+2020 dagger   "xC2x87" => "xE2x80xA1", // U+2021 double dagger   "xC2x88" => "xCBx86",     // U+02C6 modifier letter circumflex accent   "xC2x89" => "xE2x80xB0", // U+2030 per mille sign   "xC2x8A" => "xC5xA0",     // U+0160 latin capital letter s with caron   "xC2x8B" => "xE2x80xB9", // U+2039 single left-pointing angle quotation mark   "xC2x8C" => "xC5x92",     // U+0152 latin capital ligature oe   "xC2x8E" => "xC5xBD",     // U+017D latin capital letter z with caron   "xC2x91" => "xE2x80x98", // U+2018 left single quotation mark   "xC2x92" => "xE2x80x99", // U+2019 right single quotation mark   "xC2x93" => "xE2x80x9C", // U+201C left double quotation mark   "xC2x94" => "xE2x80x9D", // U+201D right double quotation mark   "xC2x95" => "xE2x80xA2", // U+2022 bullet   "xC2x96" => "xE2x80x93", // U+2013 en dash   "xC2x97" => "xE2x80x94", // U+2014 em dash   "xC2x98" => "xCBx9C",     // U+02DC small tilde   "xC2x99" => "xE2x84xA2", // U+2122 trade mark sign   "xC2x9A" => "xC5xA1",     // U+0161 latin small letter s with caron   "xC2x9B" => "xE2x80xBA", // U+203A single right-pointing angle quotation mark   "xC2x9C" => "xC5x93",     // U+0153 latin small ligature oe   "xC2x9E" => "xC5xBE",     // U+017E latin small letter z with caron   "xC2x9F" => "xC5xB8",     // 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);


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/466117.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号