Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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).</p> <pre><code>&lt;?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); </code></pre> <p>If you want to throw out all non-word characters:</p> <pre><code>&lt;?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); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload