简体   繁体   中英

Advance syllable counter with php and regex

Im making a syllable counter that hyphenates words with the following parameters

Ex.

Res olu tio n = re-so-lu-tion

Ver sat ile = Ver-sa-til

Ped ago gy = Pe-da-go-gy

Tra nsu bst ant iat ion = tran-sub-stan-ti-a-tion

Ame ric an = Ame-ri-can

Rudimentary = ru-di-men-ta-ry

Hig hli ght = high-light

Im using PHP to code it, so far, im able to break the word down:

<?php

$string = 'University';

preg_replace('/tion|[aeiou]/', "-$0", $string);

?>

Output: Un-iv-ers-ity

What it needs to do now is read the exceptions. Output after the rules are placed in.

  1. Uni
  2. ver
  3. si
  4. ty

How would i change the PHP expression to account for the required rules? Would java script perhaps be a better language?

Since breaking a word into syllables is not something you can express as a "rule", regex is not a good tool (the language doesn't matter). There are too many exceptions! The only reasonable thing will be to create a parser.

That said, for the example you posted above you can do:

preg_replace('/tion|Uni|ver|si|ty|[aeiou]/', "-$0", $string);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM