为什么这个测试用例不起作用?
<?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♥</span>', $card);
$card = preg_replace('/(\d+)♦/', '<span class="red">$1♦</span>', $card);
$card = preg_replace('/(\d+)♠/', '<span class="black">$1♠</span>', $card);
$card = preg_replace('/(\d+)♣/', '<span class="black">$1♣</span>', $card);
printf("suit: %s, html: %s\n", $suit, $card);
}
?>
输出:
suit: ▒, html: <span class="black">7♠</span>
suit: ▒, html: Д♠
suit: ▒, html: К♠
suit: ▒, html: <span class="red">8♦</span>
suit: ▒, html: В♦
suit: ▒, html: Д♦
suit: ▒, html: <span class="black">10♣</span>
suit: ▒, html: <span class="red">10♥</span>
suit: ▒, html: В♥
suit: ▒, html: Т♥
即我在我的 PHP 脚本中遇到了 2 个问题:
- 为什么没有正确提取最后一个 UTF-8 字符?
- 为什么只有第一套被替换
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♥</span>', $card);
$card = preg_replace('/(\d+)♦/u', '<span class="red">$1♦</span>', $card);
$card = preg_replace('/(\d+)♠/u', '<span class="black">$1♠</span>', $card);
$card = preg_replace('/(\d+)♣/u', '<span class="black">$1♣</span>', $card);
printf("suit: %s, html: %s\n", $suit, $card);
}
?>
新输出:
suit: ♠, html: <span class="black">7♠</span>
suit: ♠, html: Д♠
suit: ♠, html: К♠
suit: ♦, html: <span class="red">8♦</span>
suit: ♦, html: В♦
suit: ♦, html: Д♦
suit: ♣, html: <span class="black">10♣</span>
suit: ♥, html: <span class="red">10♥</span>
suit: ♥, html: В♥