2

このテスト ケースが機能しないのはなぜですか?

<?php
// cards with cyrillic inidices and suits in UTF-8 encoding
$a = array('7♠', 'Д♠', 'К♠', '8♦', 'В♦', 'Д♦', '10♣', '10♥', 'В♥', 'Т♥');
foreach ($a as $card) {
        $suit = substr($card, -1);

        $card = preg_replace('/(\d+)♥/', '<span class="red">$1&hearts;</span>', $card);
        $card = preg_replace('/(\d+)♦/', '<span class="red">$1&diams;</span>', $card);
        $card = preg_replace('/(\d+)♠/', '<span class="black">$1&spades;</span>', $card);
        $card = preg_replace('/(\d+)♣/', '<span class="black">$1&clubs;</span>', $card);

        printf("suit: %s, html: %s\n", $suit, $card);
}
?>

出力:

suit: ▒, html: <span class="black">7&spades;</span>
suit: ▒, html: Д♠
suit: ▒, html: К♠
suit: ▒, html: <span class="red">8&diams;</span>
suit: ▒, html: В♦
suit: ▒, html: Д♦
suit: ▒, html: <span class="black">10&clubs;</span>
suit: ▒, html: <span class="red">10&hearts;</span>
suit: ▒, html: В♥
suit: ▒, html: Т♥

つまり、PHP スクリプトで 2 つの問題に苦しんでいます。

  1. 最後の UTF-8 文字が正しく抽出されないのはなぜですか?
  2. なぜ最初のスーツだけが に置き換えられているのpreg_replaceですか?

PHP 5.3.3、PostgreSQL 8.4.12 を使用して、CentOS 6.2 で UTF-8 JSON (ロシア語のテキストとカード スーツを含む) を保持します。

1. が PHP 5.3.3 のバグである場合、適切な回避策はありますか? (ストックパッケージをアップグレードしたくありません)。

アップデート:

<?php
$a = array('7♠', 'Д♠', 'К♠', '8♦', 'В♦', 'Д♦', '10♣', '10♥', 'В♥', 'Т♥');
foreach ($a as $card) {
        $suit = mb_substr($card, -1, 1, 'UTF-8');

        $card = preg_replace('/(\d+)♥/u', '<span class="red">$1&hearts;</span>', $card);
        $card = preg_replace('/(\d+)♦/u', '<span class="red">$1&diams;</span>', $card);
        $card = preg_replace('/(\d+)♠/u', '<span class="black">$1&spades;</span>', $card);
        $card = preg_replace('/(\d+)♣/u', '<span class="black">$1&clubs;</span>', $card);

        printf("suit: %s, html: %s\n", $suit, $card);
}
?>

新しい出力:

suit: ♠, html: <span class="black">7&spades;</span>
suit: ♠, html: Д♠
suit: ♠, html: К♠
suit: ♦, html: <span class="red">8&diams;</span>
suit: ♦, html: В♦
suit: ♦, html: Д♦
suit: ♣, html: <span class="black">10&clubs;</span>
suit: ♥, html: <span class="red">10&hearts;</span>
suit: ♥, html: В♥
4

1 に答える 1

10

substrは、1 バイト = 1 文字を想定する単純な PHP コア関数の 1 つです。substr(..., -1)文字列から最後のバイトを抽出します。ただし、「♠」は 1 バイトよりも長いです。mb_substr($card, -1, 1, 'UTF-8')代わりに使用する必要があります。

u(PCRE_UTF8) 修飾子を正規表現に追加して、UTF-8 でエンコードされた表現と文字列を正しく処理できるようにする必要があります。

preg_replace('/(\d+)♥/u', ...
于 2012-06-16T11:51:20.847 に答える