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

Example Interest Rates: Home Loan Variable: 1.89% (1.9%*) • Home Loan Fixed: 1.84% (2.11%*) • Fixed: 1.84% (2.11%*) • Variable: 1.89% (1.9%*) • Investment IO: 2.05% (4.36%*) • Investment PI: 1.58% (2.72%*)

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

See also: PHP Text Statistics (on Github ).

Download our 650-page guide on Finance Marketing. We'll show you exactly how we generate Billions in volume for our clients.

  AUS Eastern Standard Time (Connecticut)

  Want to have a chat?
 

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