计算UTF8字符串中的字母数和数字


Count number of letters-numbers in an UTF8 string?

基本上我有这个字符串,例如$str = 'По, своей 12'我需要一些返回字符和数字(去掉空格和其他标点符号)的东西

我怎样才能做到这一点?也许用'p{L}[0-9]做一个preg_replace?

countChars('По, своей 12'); //> Should return: 9

注意:mb_strlen()也计算空格和标点符号,我不想要这个

我做到了:

function countChars($haystack) {
    $count = preg_replace("/[^'p{L}0-9]/uim",'',$haystack);
    return mb_strlen($count);
}