テキストを処理するときに、すべてのタイプのスマートクオートを通常のクオートに変換しようとしています。しかし、私がコンパイルした次の関数はまだサポートと適切な設計が不足しているようです。
すべての 引用文字 を適切に変換する方法を誰かが知っていますか?
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文字は厳密に1つ "一般カテゴリ" に属し、引用文字を含むことができる文字は次のとおりです。
Ps
"句読点、開く"Pe
"句読点、閉じる"Pi
"句読点、最初の引用(使用方法によってはPsまたはPeのように動作する場合があります)"Pf
"句読点、最後の引用(使用方法によってはPsまたはPeのように動作する場合があります)"Po
"句読点、その他"(これらのページは、見逃していないことを確認するのに便利です。 カテゴリのインデックス もあります)
Unicode対応の正規表現で これらのカテゴリに一致する を使用すると便利な場合があります。
さらに、Unicode文字には "properties" があり、そのうち興味のあるものは _Quotation_Mark
_ です。残念ながら、これらは正規表現ではアクセスできません。
ウィキペディアで _Quotation_Mark
_プロパティを持つ文字のグループ を見つけることができます。最終的な参照はunicode.orgの PropList.txt ですが、これはASCIIテキストファイルです。
CJK文字も変換する必要がある場合は、コードポイントを取得し、変換を決定し、たとえばfileformat.infoで検索することにより、UTF-8エンコーディングを見つけるだけです(たとえば、U + 301Eの場合:- http://www.fileformat.info/info/unicode/char/301e/index.htm )。
Windowsコードページ1252に関して: nicode は ISO-8859-1 とまったく同じ文字を表す最初の256コードポイントを定義しますが、ISO-8859-1はしばしば-と混同されます- Windowsコードページ1252 。すべてのブラウザは、Windowsコードページ1252であるかのように、0x80-0x9Fの範囲をレンダリングします。これは、ISO-8859-1では「空」です(より正確には、制御文字が含まれています)。 Wikipediaページの表 は、Unicodeに相当するものをリストしています。
注: strtr()
は、多くの場合 str_replace()
よりも低速です。入力とPHPバージョン。それが十分に高速であれば、my _$chr_map
_のようなマップを直接使用できます。
入力が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);
_
この1つの関数を使用して、すべての文字を変換できます。
$output = iconv('UTF-8', 'ASCII//TRANSLIT', $input);
必ずタイプを必要なものに変更してください。
(注:これは、見つかった別の同様の質問 here からのものです)。