替换PHP5.3中的UTF-8字符
为什么这个测试用例不起作用替换PHP5.3中的UTF-8字符,php,utf-8,preg-replace,php-5.3,Php,Utf 8,Preg Replace,Php 5.3,为什么这个测试用例不起作用 <?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('/(\
<?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: В♥
套装:♠, html:7&spades;
诉讼:♠, html:crmk♠
诉讼:♠, html:k♠
诉讼:♦, html:8&diams;
诉讼:♦, html:b♦
诉讼:♦, html:crmk♦
诉讼:♣, html:10个及以上会社;;
诉讼:♥, html:10&hearts;
诉讼:♥, html:b♥
substr
是一个天真的PHP核心函数,它假设1字节=1个字符substr(…,-1)
从字符串中提取最后一个字节。"♠" 但长度超过一个字节。您应该改用mb\u substr($card,-1,1,'UTF-8')
您需要将添加到正则表达式中,以使其正确处理UTF-8编码的表达式和字符串:
preg_replace('/(\d+)♥/u', ...
谢谢,第一个问题是通过调用
mb_substr
解决的。但是第二个问题不是通过在模式中添加/u
来解决/更改的。我已经用新脚本和输出更新了我的问题。\d
只匹配一个数字。请改用
匹配任何字符。
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: В♥
preg_replace('/(\d+)♥/u', ...