1
私は文字列の句読点を数えるのにこの簡単なコードを持っています。つまり「2つのカンマ、3つのセミコロン...」などですが、emダッシュ( - )が表示されても機能しません。それはハイフン( - )ではないことに注意してください。私はそれらを気にしません。句読点付きの奇妙な連想配列
em-dashには、PHP文字列や配列キーとして奇妙なものがありますか?おそらく奇妙なユニコードの問題?
$punc_counts = array(
"," => 0,
";" => 0,
"—" => 0, //exists, really!
"'" => 0,
"\"" => 0,
"(" => 0,
")" => 0,
);
// $str is a long string of text
//remove all non-punctuation chars from $str (works correctly, keeping em-dashes)
$puncs = "";
foreach($punc_counts as $key => $value)
$puncs .= $key;
$str = preg_replace("/[^{$puncs}]/", "", $str);
//$str now equals something like:
//$str == ",;'—\"—()();;,";
foreach(str_split($str) as $char)
{
//if it's a puncutation char we care about, count it
if(isset($punc_counts[$char]))
$punc_counts[$char]++;
else
print($char);
}
print("<br/>");
print_r($punc_counts);
print("<br/>");
印刷物上のコード:
——
Array ([,] => 2 [;] => 3 [—] => 0 ['] => 1 ["] => 1 [(] => 2 [)] => 2)