Why doesn't this test case work?
<?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);
}
?>
Output:
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: Т♥
Ie I'm struggling with 2 problems in my PHP-script:
preg_replace
? Using PHP 5.3.3, PostgreSQL 8.4.12 holding UTF-8 JSON (with Russian text and card suits) on CentOS 6.2.
If 1. is a bug in PHP 5.3.3, then is there a good workaround? (I don't want to upgrade the stock package).
UPDATE:
<?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);
}
?>
The new output:
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: В♥
substr
is one of the naïve PHP core functions which assumes 1 byte = 1 character. substr(..., -1)
extracts the last byte from the string. "♠" is longer than one byte though. You should use mb_substr($card, -1, 1, 'UTF-8')
instead.
You need to add the u
(PCRE_UTF8) modifier to the regular expression to make it deal with UTF-8 encoded expressions and strings correctly:
preg_replace('/(\d+)♥/u', ...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.