forse potrà servire e sicuramente migliorare.
Codice PHP:
<?php
/**
* Hyphenation - sillabatore
* PHP Library for the hyphenation
* @author Raffaello Regoli <info@modografico.com>
* @copyright Copyright (c) 2014, Raffaello Regoli
*/
function isV($str){
$v="aeiouy";
for($n=0;$n<strlen($v);$n++){
if($str==$v{$n})return true;
}
}
function isC($str){
$v="bcdfgjpqstvwxz";
for($n=0;$n<strlen($v);$n++){
if($str==$v{$n})return true;
}
}
function isL($str){
$v="hlmnr";
for($n=0;$n<strlen($v);$n++){
if($str==$v{$n})return true;
}
}
function isN($str){
$v="0123456789";
for($n=0;$n<strlen($v);$n++){
if($str==$v{$n})return true;
}
}
function doHYPHEN($string, $start){
$len=strlen($string);
for($n=$start;$n<$len-1;$n++){
$a=strtolower($string{$n});
$b=strtolower($string{$n+1});
$c=strtolower($string{$n+2});
if((!isN($a) and !isN($b)) or $a!="s"){
if($a==" "){//è uno spazio
$hyphen[0]=substr($string, 0, $n+1);
$hyphen[1]=substr($string, $n+1);
return $hyphen;
}
//sono lettere doppie
if($a==$b and $n<$len-2 and $c!=" "){
$hyphen[0]=substr($string, 0, $n+1)."-";
$hyphen[1]=substr($string, $n+1);
return $hyphen;
}
//vocale + s
if(isV($a) and $b=="s"){
$hyphen[0]=substr($string, 0, $n+1)."-";
$hyphen[1]=substr($string, $n+1);
return $hyphen;
}
//liquida + s
if(isL($a) and $b=="s"){
$hyphen[0]=substr($string, 0, $n+1)."-";
$hyphen[1]=substr($string, $n+1);
return $hyphen;
}
//vocale + consonante + vocale
if(isV($a) and isC($b) and isV($c)){
$hyphen[0]=substr($string, 0, $n+1)."-";
$hyphen[1]=substr($string, $n+1);
return $hyphen;
}
//vocale + consonante + liquida
if(isV($a) and isC($b) and isL($c)){
$hyphen[0]=substr($string, 0, $n+1)."-";
$hyphen[1]=substr($string, $n+1);
return $hyphen;
}
//liquida + consonante + vocale
if(isL($a) and isC($b) and isV($c)){
$hyphen[0]=substr($string, 0, $n+1)."-";
$hyphen[1]=substr($string, $n+1);
return $hyphen;
}
//liquida + consonante + liquida
if(isL($a) and isC($b) and isL($c)){
$hyphen[0]=substr($string, 0, $n+1)."-";
$hyphen[1]=substr($string, $n+1);
return $hyphen;
}
//vocale + liquida + vocale
if(isV($a) and isL($b) and isV($c)){
$hyphen[0]=substr($string, 0, $n+1)."-";
$hyphen[1]=substr($string, $n+1);
return $hyphen;
}
//liquida + liquida + vocale
if(isL($a) and isL($b) and isV($c)){
$hyphen[0]=substr($string, 0, $n+1)."-";
$hyphen[1]=substr($string, $n+1);
return $hyphen;
}
}
}
$hyphen[0]=$string;
return $hyphen;
}
function hyphenation($str, $len){
$a=doHYPHEN($str, $len);
$strHYP="$a[0]";
while(strlen($a[1])>$len){
$a=doHYPHEN($a[1], $len);
$strHYP.="<br>$a[0]";
}
$strHYP.="<br>$a[1]";
return $strHYP;
}
function troncaparola($str, $len){
$a=doHYPHEN($str, $len);
$strHYP="$a[0]";
//while(strlen($a[1])>$len){
//$a=doHYPHEN($a[1], $len);
//$strHYP.="<br>$a[0]";
//}
$strHYP.="<br>$a[1]";
return $strHYP;
}
/*
//per sillabare una parola:
//es.:
$string="ermafrodito";
$len=0;
$word=hyphenation($string, $len);
print($word);
//per troncare una parola:
//es.:
$string="ermafrodito";
$pos=2;
$word=troncaparola($string, $pos);
print($word);
*/
?>