* -> if you don’t want to exclude
snippets
* WordPress example:
* post_content); ?>
*
* BTW, this software is free.
* by Creative Nights
*
*/
function cn_article_readability($text, $remove_PRE = true) {
if ($remove_PRE) {
$text = preg_replace('/pre>(.*)?<\/pre/si','', $text);
}
function countSentences($x){
$find = array('Mr.', 'Mrs.', 'Ms.', 'i.e.', 'e.g.', 'vs.');
$x = str_replace($find,'',$x);
return preg_match_all('/[^\s](\.|\!|\?)(?!\w)/', $x, $match);
}
// Based on http://mikebrum.com/counting-the-syllables-in-a-word-with-php/
function countSyllables($x) {
$triples = 'dn\’t|dn\'t|eau|iou|ouy|you|bl\s';
$doubles = 'ai|ae|ay|au|ea|ee|ei|eu|ey|ie|ii|io|oa|oe|oi|oo|ou|oy|ue|uy|ya|ye|yi|yo|yu';
$singles = 'a|e|i|o|u|y';
// Cleaning up word endings
$find = array('s ','e ');
$x = str_replace($find, '', $x);
return preg_match_all('/(' . $triples . '|' . $doubles . '|' . $singles . ')/i', $x, $match);
}
$find = array(' ','.','!','?',"\n");
$characters = strlen(str_replace($find, '', $text));
$syllables = countSyllables($text);
$words = str_word_count($text);
$sentences = countSentences($text);
$mins = floor($words / 200);
$secs = floor($words % 200 / (200 / 60));
// Formulas
$reading_time = $mins . ' minute' . ($mins == 1 ? '' : 's') . ', ' . $secs . ' second' . ($secs == 1 ? '' : 's');
// http://en.wikipedia.org/wiki/Automated_Readability_Index
$readability_index = (4.71 * ($characters/$words)) + (0.5 * ($words/$sentences)) - 21.43;
$readability_index = number_format($readability_index,2);
// http://en.wikipedia.org/wiki/Flesch-Kincaid_Readability_Test
$reading_ease_score = (206.835 - 1.015*($words/$sentences) - 84.6*($syllables/$words));
$reading_ease_score = number_format($reading_ease_score,2);
// Output everything
$output = '';
// $output .= $text;
$output .= 'Estimated reading time
';
$output .= $reading_time . '
';
$output .= 'Automated readability index
';
$output .= $readability_index . ' (lower is better)
';
$output .= 'The Flesch-Kincaid reading ease
';
$output .= $reading_ease_score . ' out of 120 (higher is better)
';
$output .= '';
$output .= 'Characters | ' . $characters . ' |
';
$output .= 'Syllables | ' . $syllables . ' |
';
$output .= 'Words | ' . $words . ' |
';
$output .= 'Sentences | ' . $sentences . ' |
';
$output .= '
';
return $output;
}
?>