Skip to content

Instantly share code, notes, and snippets.

@msisaifu
Created November 11, 2018 18:26
Show Gist options
  • Save msisaifu/cbfe983b6f991d2182ba764d8b5c56f8 to your computer and use it in GitHub Desktop.
Save msisaifu/cbfe983b6f991d2182ba764d8b5c56f8 to your computer and use it in GitHub Desktop.
Counts all the character of a string ascending order
<?php
$string = "hello!world!!! rrr";
echo "<pre>";
print_r(countChar($string));
echo "</pre>";
function countChar($string)
{
$explodeString = str_split(strtolower($string));
$countChar = array_count_values($explodeString);
ksort($countChar);
foreach ($countChar as $key => $value)
{
$asciiVal=ord($key);
if (
($asciiVal>=ord(' ') && $asciiVal<=ord('/')) ||
($asciiVal>=ord(':') && $asciiVal<=ord('`')) ||
($asciiVal>=ord('{') && $asciiVal<=ord('~')))
{
unset($countChar[$key]);
}
}
return $countChar;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment