Skip to content

Instantly share code, notes, and snippets.

@p4ulypops
Created January 31, 2014 22:49
Show Gist options
  • Save p4ulypops/8744850 to your computer and use it in GitHub Desktop.
Save p4ulypops/8744850 to your computer and use it in GitHub Desktop.
<?php
//
//
// http://skilleo.me/challenge/6
//
//
function textAnalytics($a) {
/* Create the code to gather the text analytics here. */
$return = "";
// Number of words ----- Need to take one away for some reason, your counter is wrong.
$return .= (str_word_count($a)).', ';
// Number of Letters
$return .= strlen(preg_replace('![^a-zA-Z]+!', '', $a)).", ";
// White spaces
$return .= strlen(preg_replace('![a-zA-Z0-9\s]+!', '', $a)).", ";
// Words used two or more
$return .= myWordCounter($a, "first").", ";
// Letters used more than 3 times
$return .= myLetterCounter($a).", ";
//words used only once
// "this is a sample text!" is 5 words but you want 4.
$return .= (myWordCounter($a, "second"));
return $return;
}
function myLetterCounter($string) {
$string = preg_replace("![^a-zA-Z]+!", "", $string);
$array = str_split($string);
$tmp = [];
foreach($array as $l) {
$tmp[strtolower($l)]++;
}
$x = 0;
foreach($tmp as $c) {
if ($c >2) {$x++;}
}
// return print_r($tmp, true);
return $x;
}
function myWordCounter($string, $mode) {
$words = explode(" ", preg_replace("/[^ \w]+/", "", $string));
$tmp = [];
foreach($words as $w) {
$tmp[$w]++;
}
$x = 0;
// return print_r($tmp, true);
foreach($tmp as $k => $v) {
if ($mode == "first") {
if ($v >= 2) { $x++; }
} else {
if ($v == 1) { $x++; }
}
}
return $x;
}
$__fp = fopen("php://stdin", "r");
$_a = fgets($__fp);
$res = textAnalytics($_a);
echo $res;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment