PLEASE NOTE: This code is now considered out of date. An updated version has been released under an open source license as a Google Code project: php-text-statistics. There is more about this change in the post Readability Code Open Sourced.
A tool for checking the readability scores of text is available - this article covers the functions behind that tool.
The Gunning-Fog index is a measure of text readability. It represents the approximate reading age of the text - the age someone will need to be to understand what they are reading.
The following is the algorithm to determine the Gunning-Fog index:
(average_words_sentence + percentage_of_words_with_more_than_three_syllables) * 0.4
The above produces a number, which is a rough measure of the age someone must be to understand the content. The lower the number, the more understandable the content will be to your visitors. Web sites should aim to have content that falls roughly in the 11-15 range for this test.
Any number returned over the value of 22 can be taken to be just 22, and is roughly equivalent to post-graduate level.
Below are a selection of function you can use to determine the Gunning-Fog index of text. To calculate this, all you need to is call the function as follows, where $text is the text you wish to measure the readability of.
$gunning_fog_score = gunning_fog_score($text);
function gunning_fog_score($text) {
return ((average_words_sentence($text) + percentage_number_words_three_syllables($text)) * 0.4);
}
function average_words_sentence($text) {
$sentences = strlen(preg_replace('/[^\.!?]/', '', $text));
$words = strlen(preg_replace('/[^ ]/', '', $text));
return ($words/$sentences);
}
function percentage_number_words_three_syllables($text) {
$syllables = 0;
$words = explode(' ', $text);
for ($i = 0; $i < count($words); $i++) {
if (count_syllables($words[$i]) > 2) {
$syllables ++;
}
}
$score = number_format((($syllables / count($words)) * 100));
return ($score);
}
function count_syllables($word) {
$subsyl = Array(
'cial'
,'tia'
,'cius'
,'cious'
,'giu'
,'ion'
,'iou'
,'sia$'
,'.ely$'
);
$addsyl = Array(
'ia'
,'riet'
,'dien'
,'iu'
,'io'
,'ii'
,'[aeiouym]bl$'
,'[aeiou]{3}'
,'^mc'
,'ism$'
,'([^aeiouy])\1l$'
,'[^l]lien'
,'^coa[dglx].'
,'[^gq]ua[^auieo]'
,'dnt$'
);
// Based on Greg Fast's Perl module Lingua::EN::Syllables
$word = preg_replace('/[^a-z]/is', '', strtolower($word));
$word_parts = preg_split('/[^aeiouy]+/', $word);
foreach ($word_parts as $key => $value) {
if ($value <> '') {
$valid_word_parts[] = $value;
}
}
$syllables = 0;
// Thanks to Joe Kovar for correcting a bug in the following lines
foreach ($subsyl as $syl) {
$syllables -= preg_match('~'.$syl.'~', $word);
}
foreach ($addsyl as $syl) {
$syllables += preg_match('~'.$syl.'~', $word);
}
if (strlen($word) == 1) {
$syllables++;
}
$syllables += count($valid_word_parts);
$syllables = ($syllables == 0) ? 1 : $syllables;
return $syllables;
}









7 Comments
what type of words help build up decent gunning score.
#1, paul.shann@bigpond.com, Australia, 19 April 2006. Reply to this.
The phrase "Gunning-Fog Index." gives:
Flesch-Kincaid Reading Ease: -6
Ideally, web page text should be around the 60 to 80 mark on this scale. The higher the score, the more readable the text.
Flesch-Kincaid Grade Level: 14
Ideally, web page text should be around the 6 to 7 mark on this scale. The lower the score, the more readable the text.
Gunning-Fog Index: 25
Ideally, web page text should be between 11 and 15 on this scale. The lower the score, the more readable the text. (Anything over 22 should be considered the equivalent of post-graduate level text).
So G-F indices are post-graduate-level. Interesting.
#2, Anonymous, Republic Of Korea, 26 August 2006. Reply to this.
For fun (and functionality), I'm coding a small real-time implementation of Readability tests and noticed an inconsistency in your formula; why are you adding 5 to the calculated index?
#3, Martijn W. van der Lee, Netherlands, 4 September 2006. Reply to this.
Hi Martijn,
I wrote this so long ago, I honestly don't remember why I added 5. I can't think of a reason now, either, so I've removed it.
#4, Dave Child, United Kingdom, 5 September 2006. Reply to this.
Thanks for this and the FK algo. They were dividing by zero if passed text bracketed by phpBB code [color=red]some text[/color]. I added the max(1, ...) to prevent zero sentences and zero words in two slightly modified functions below.
function average_words_sentence( $text )
{
$sentences = max( 1, strlen( preg_replace( '/[^\.!?]/', '', $text ) ) );
$words = strlen( preg_replace( '/[^ ]/', '', $text ) );
return ( $words / $sentences );
}
function percentage_number_words_three_syllables( $text )
{
$syllables = 0;
$words = explode( ' ', $text );
for ( $i = 0; $i < count( $words ); $i++ )
{
if ( count_syllables( $words[$i] ) > 2 )
{
$syllables ++;
}
}
$score = number_format( ( ( $syllables / max( 1, count( $words ) ) ) * 100 ) );
return ( $score );
}
#5, Michael Brenden, Unknown, 6 February 2008. Reply to this.
It is mentioned that any number returned over the value of 22 can be taken to be just 22, and is roughly equivalent to graduate level. My point is that any criteria to judge that the value of 22 is equivalent to or suitable for graduate students. I need to know. Anyone can answer? I am working on my research about this point.
#6, Natjiree J., Thailand, 7 May 2008. Reply to this.
He came into the room to shut the windows while we were still in bed and
saw he looked ill.He was shivering,his face was white,and he walked
slowly as though it ached to move.
'What's the matter,Schatz?'
'I've got a headache.'
'You better go back to bed.'
'No,I'm all right.'
'You go to bed.I'll see you when I'm dressed.'
But when I came downstairs he was dressed,sitting by the fire,looking a
very sick and miserable boy of nine years.When I put my hand on his
forehead I knew he had a fever.
'You go up to bed,'I said,'you're sick.'
'I'm all right,'he said.
When the doctor came he took the boy's temperature.
'What is it?'I asked him.
'One hundred and two.'
Downstairs,the doctor left three different medicines in different colored
capsules with instructions for giving them.One was to bring down the feve
another a purgative,the third to overcome an acid condition.The germs o
influenza can only exist in an acid condition,he explained.He seemed to
know all about influenza and said there was nothing to worry about if the
fever did not go above one hundred and four degrees.This was a light
epidemic of flu and there was no danger if you avoided pneumonia.
Back in the room I wrote the boy's temperature down and made a note of
the time to give the various capsules.
'Do you want me to read to you?'
'All right.If you want to,'said the boy.His face was very white and there
were dark areas under his eyes.He lay still in bed and seemed very
detached from what was going on.
I read aloud from Howard Pyle's Book of Pirates;but I could see he was n
following what I was reading.
'How do you feel,Schatz?'I asked him.
'Just the same,so far,'he said.
I sat at the foot of the bed and read to myself while I waited for it to be tim
to give another capsule.It would have been natural for him to go to sleep,
but when I looked up he was looking at the foot of the bed,looking very
strangely.
'Why don't you try to go to sleep?I'll wake you up for the medicine.'
'I'd rather stay awake.'
After a while he said to me,'You don't have to stay here with me,Papa,if
bothers you.'
'It doesn't bother me.'
'No,I mean you don't have to stay if it's going to bother you.'
I thought perhaps he was a little light-headed and after giving him the
prescribed capsule at eleven o'clock I went out for a while.
It was a bright,cold day,the ground covered with a sleet that had frozen s
that it seemed as if all the bare trees,the bushes,the cut brush and all the
grass and the bare ground had been varnished with ice.I took the young
Irish setter for a little walk up the road and along a frozen creek,but it was
difficult to stand or walk on the glassy surface and the red dog slipped and
slithered and fell twice,hard,once dropping my gun and having it slide ovthe ice.
We flushed a covey of quail under a high clay bank with overhanging brus
and killed two as they went out of sight over the top of the bank.Some of
the covey lit the trees,but most of them scattered into brush piles and it w
necessary to jump on the ice-coated mounds of brush several times befor
they would flush.Coming out while you were poised unsteadily on the icy,
springy brush they made difficult shooting and killed two,missed five,and
started back pleased to have found a covey close to the house and happy
there were so many left to find on another day.
At the house they said the boy had refused to let anyone come into the
room.
'You can't come in,'he said.'You mustn't get what I have.'
I went up to him and found him in exactly the position I had left him,white
faced,but with the tops of his cheeks flushed by the fever,staring still,as
he had stared,at the foot of the bed.
I took his temperature.
'What is it?'
'Something like a hundred,'I said.It was one hundred and two and four
tenth.
'It was a hundred and two,'he said.
'Who said so?'
'The doctor.'
'Your temperature is all right,'I said.It's nothing to worry about.'
'I don't worry,'he said,'but I can't keep from thinking.'
'Don't think,'I said.'Just take it easy.'
'I'm taking it easy,'he said and looked straight ahead.He was evidently
holding tight onto himself about something.
'Take this with water.'
'Do you think it will do any good?'
'Of course it will.'
I sat down and opened the Pirate book and commenced to read,but I cou
see he was not following,so I stopped.
'About what time do you think I'm going to die?'he asked.
'What?'
'About how long will it be before I die?'
'You aren't going to die.What's the matter with you?'
Oh,yes,I am.I heard him say a hundred and two.'
'People don't die with a fever of one hundred and two.That's a silly way to
talk.'
'I know they do.At school in France the boys told me you can't live with
forty-four degrees.I've got a hundred and two.'
He had been waiting to die all day,ever since nine o'clock in the morning.
'You poor Schatz,'I said.'Poor old Schatz.It's like miles and kilometers.
You aren't going to die.That's a different thermometer.On that
thermometer thirty-seven is normal.On this kind it's ninety-eight.'
'Are you sure?'
'Absolutely,'I said.'It's like miles and kilometers.You know,like how man
kilometers we make when we do seventy in the car?'
'Oh,'he said.
But his gaze at the foot of his bed relaxed slowly.The hold over himself
relaxed too,finally,and the next day it was very slack and he cried very
easily at little things that were of no importance.
#7, Anonymous, 16 March 2010. Reply to this.