Australia's Leading Digital Marketing Experts. T. 1300 235 433  |  Aggreagtion Enquires Welcome

Count The Number Of Syllables in a Word With PHP

This PHP function to count syllables in a word isn't entirely reliable, but we're providing it despite its lack of accuracy. It supports, in part, the article published here (Count Sentences, Words, and Syllables With PHP).

1
<?php 
2
/*
3
    Count the number of syllables in a word
4
    http://www.beliefmedia.com/count-sentences-syllables-php
5
*/
6
 
7
 
8
function beliefmedia_count_syllables($word) { 
9
 
10
  $subsyl = Array('cial','tia','cius','cious','giu','ion','iou','sia$','.ely$'); 
11
  $addsyl = Array('ia','riet','dien','iu','io','ii','[aeiouym]bl$','[aeiou]{3}','^mc','ism$','([^aeiouy])\1l$','[^l]lien','^coa[dglx].','[^gq]ua[^auieo]','dnt$'); 
12
 
13
  /* Based on Greg Fast's Perl module Lingua::EN::Syllables */
14
  $word = preg_replace('/[^a-z]/is', '', strtolower($word)); 
15
  $word_parts = preg_split('/[^aeiouy]+/', $word);
16
 
17
  foreach ($word_parts as $key => $value) { 
18
    if ($value <> '') { 
19
      $valid_word_parts[] = $value; 
20
    } 
21
  } 
22
 
23
  $syllables = 0; 
24
  foreach ($subsyl as $syl) { 
25
    if (strpos($word, $syl) !== false) { 
26
      $syllables--; 
27
    } 
28
  }
29
 
30
  foreach ($addsyl as $syl) { 
31
    if (strpos($word, $syl) !== false) { 
32
      $syllables++; 
33
    } 
34
  }
35
 
36
  if (strlen($word) == 1) { 
37
    $syllables++; 
38
  }
39
 
40
  $syllables += count($valid_word_parts); 
41
  $syllables = ($syllables == 0) ? 1 : $syllables; 
42
  
43
 return $syllables; 
44
}

See also: PHP Text Statistics (on Github ).

Like this article?

Share on facebook
Share on Facebook
Share on twitter
Share on Twitter
Share on linkedin
Share on Linkdin
Share on pinterest
Share on Pinterest

Leave a comment