多字节安全计数字符串中的不同字符

发布于 2024-12-09 13:28:06 字数 357 浏览 0 评论 0原文

我不想找到一种智能且有效的方法来计算一个字符串中有多少个不同的字母字符。示例:

$str = "APPLE";
echo char_count($str) // should return 4, because APPLE has 4 different chars 'A', 'P', 'L' and 'E'

$str = "BOB AND BOB"; // should return 5 ('B', 'O', 'A', 'N', 'D'). 

$str = 'PLÁTANO'; // should return 7 ('P', 'L', 'Á', 'T', 'A', 'N', 'O')

它应该支持 UTF-8 字符串!

I wan't to find a smart and efficient way of counting how many different alpha characters are in one string. Example:

$str = "APPLE";
echo char_count($str) // should return 4, because APPLE has 4 different chars 'A', 'P', 'L' and 'E'

$str = "BOB AND BOB"; // should return 5 ('B', 'O', 'A', 'N', 'D'). 

$str = 'PLÁTANO'; // should return 7 ('P', 'L', 'Á', 'T', 'A', 'N', 'O')

It should support UTF-8 strings!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

生寂 2024-12-16 13:28:06

如果您正在处理 UTF-8(您确实应该考虑这一点,恕我直言),则所有发布的解决方案(使用 strlen、str_split 或 count_chars)都不起作用,因为它们都将一个字节视为一个字符(对于显然是 UTF-8)。

<?php

$treat_spaces_as_chars = true;
// contains hälöwrd and a space, being 8 distinct characters (7 without the space)
$string = "hällö wörld"; 
// remove spaces if we don't want to count them
if (!$treat_spaces_as_chars) {
  $string = preg_replace('/\s+/u', '', $string);
}
// split into characters (not bytes, like explode() or str_split() would)
$characters = preg_split('//u', $string, -1, PREG_SPLIT_NO_EMPTY);
// throw out the duplicates
$unique_characters = array_unique($characters);
// count what's left
$numer_of_characters = count($unique_characters);

如果你想扔掉所有非单词字符:

<?php

$ignore_non_word_characters = true;
// contains hälöwrd and PIE, as this is treated as a word character (Greek)
$string = "h,ä*+l•π‘°’lö wörld"; 
// remove spaces if we don't want to count them
if ($ignore_non_word_characters) {
  $string = preg_replace('/\W+/u', '', $string);
}
// split into characters (not bytes, like explode() or str_split() would)
$characters = preg_split('//u', $string, -1, PREG_SPLIT_NO_EMPTY);
// throw out the duplicates
$unique_characters = array_unique($characters);
// count what's left
$numer_of_characters = count($unique_characters);

var_dump($characters, $unique_characters, $numer_of_characters);

If you're dealing with UTF-8 (which you really should consider, imho) none of the posted solutions (using strlen, str_split or count_chars) will work, as all of them treat one byte as one character (which is not true for UTF-8, obviously).

<?php

$treat_spaces_as_chars = true;
// contains hälöwrd and a space, being 8 distinct characters (7 without the space)
$string = "hällö wörld"; 
// remove spaces if we don't want to count them
if (!$treat_spaces_as_chars) {
  $string = preg_replace('/\s+/u', '', $string);
}
// split into characters (not bytes, like explode() or str_split() would)
$characters = preg_split('//u', $string, -1, PREG_SPLIT_NO_EMPTY);
// throw out the duplicates
$unique_characters = array_unique($characters);
// count what's left
$numer_of_characters = count($unique_characters);

If you want to throw out all non-word characters:

<?php

$ignore_non_word_characters = true;
// contains hälöwrd and PIE, as this is treated as a word character (Greek)
$string = "h,ä*+l•π‘°’lö wörld"; 
// remove spaces if we don't want to count them
if ($ignore_non_word_characters) {
  $string = preg_replace('/\W+/u', '', $string);
}
// split into characters (not bytes, like explode() or str_split() would)
$characters = preg_split('//u', $string, -1, PREG_SPLIT_NO_EMPTY);
// throw out the duplicates
$unique_characters = array_unique($characters);
// count what's left
$numer_of_characters = count($unique_characters);

var_dump($characters, $unique_characters, $numer_of_characters);
醉酒的小男人 2024-12-16 13:28:06

只需使用 count_chars

echo count(array_filter(count_chars($str)));

count_chars()< 返回的数组/code> 还会告诉您字符串中每个字符有多少个。

Just use count_chars:

echo count(array_filter(count_chars($str)));

The array returned from count_chars() will also tell you how many of each character are in the string.

葬花如无物 2024-12-16 13:28:06

count_chars 返回所有 ASCII 字符的映射,告诉您字符串中每个字符的数量。这是您自己实施的起点。

function countchars($str, $ignoreSpaces) {
  $map = array();
  $len = strlen($str);
  for ($i=0; $i < $len; $i++) {
    if (!isset($map[$str{$i}])) {
      $map[$str{$i}] = 1;
    } else {
      $map[$str{$i}]++;
    }    
  }

  if ($ignoreSpaces) {
    unset($map[' ']);
  }

  return $map;
}

print_r(countchars('Hello World'));

count_chars returns a map of all the ascii characters, telling you how many of each there are in the string. Here's a starting point for your own implementation.

function countchars($str, $ignoreSpaces) {
  $map = array();
  $len = strlen($str);
  for ($i=0; $i < $len; $i++) {
    if (!isset($map[$str{$i}])) {
      $map[$str{$i}] = 1;
    } else {
      $map[$str{$i}]++;
    }    
  }

  if ($ignoreSpaces) {
    unset($map[' ']);
  }

  return $map;
}

print_r(countchars('Hello World'));
趴在窗边数星星i 2024-12-16 13:28:06

这是一个使用关联数组的魔力来完成此操作的函数。以线性时间工作。 (大O = log(n)

function uniques($string){
   $arr = array();
   $parts = str_split($string);
   foreach($parts as $part)
      $arr["$part"] = "yup";
   return count($arr);
}

$str = "APPLE";
echo uniques($str);  // outputs 4

Here's a function that will do it, using the magic of associative arrays. Works in linear time. (big O = log(n))

function uniques($string){
   $arr = array();
   $parts = str_split($string);
   foreach($parts as $part)
      $arr["$part"] = "yup";
   return count($arr);
}

$str = "APPLE";
echo uniques($str);  // outputs 4
贪恋 2024-12-16 13:28:06

我的看法是,

$chars = array_count_values(str_split($input));

这将为您提供一个唯一字母的关联数组作为键,出现次数作为值。

如果你对出现的次数不感兴趣,

$chars = array_unique(str_split($input));
$numChars = count($chars);

My take on it,

$chars = array_count_values(str_split($input));

This will give you an associative array of unique letters as the key, and the number of occurrences as the value.

If you are not interested in the number of occurrences,

$chars = array_unique(str_split($input));
$numChars = count($chars);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文