文字列をハイフネーションするコードをラテン語の詩に書き込もうとしています。それにはいくつかの制約がありますが、私は希望の出力を得られません。私のコードは以下の通りです:複数の正規表現を1つに結合する
<?php
$string = "impulerittantaenanimis caelestibusirae";
$precedingC = precedingConsonant($string);
$xrule = xRule($precedingC);
$consonantc = consonantCT($xrule);
$consonantp = consonantPT($consonantc);
$cbv = CbetweenVowels($consonantp);
$tv = twoVowels($cbv);
echo $tv;
function twoVowels($string)
{
return preg_replace('/([aeiou])([aeiou])/', '$1-$2', $string);
}
function CbetweenVowels($string)
{
return preg_replace('/([aeiou])([^aeiou])([aeiou])/', '$1-$2$3', $string);
}
function consonantPT($string)
{
return preg_replace('/([^aeiou]p)(t[aeiou])/', '$1-$2', $string);
}
function consonantCT($string)
{
return preg_replace('/([^aeiou]c)(t[aeiou])/', '$1-$2', $string);
}
function precedingConsonant($string)
{
$arr1 = str_split($string);
$length = count($arr1);
for($j=0;$j<$length;$j++)
{
if(isVowel($arr1[$j]) && !isVowel($arr1[$j+1]) && !isVowel($arr1[$j+2]) && isVowel($arr1[$j+3]))
{
$pc++;
}
}
function strAppend2($string)
{
$arr1 = str_split($string);
$length = count($arr1);
for($i=0;$i<$length;$i++)
{
$check = $arr1[$i+1].$arr1[$i+2];
$check2 = $arr1[$i+1].$arr1[$i+2].$arr1[$i+3];
if($check=='br' || $check=='cr' || $check=='dr' || $check=='fr' || $check=='gr' || $check=='pr' || $check=='tr' || $check=='bl' || $check=='cl' || $check=='fl' || $check=='gl' || $check=='pl' || $check=='ch' || $check=='ph' || $check=='th' || $check=='qu' || $check2=='phl' || $check2=='phr')
{
if(isVowel($arr1[$i]) && !isVowel($arr1[$i+1]) && !isVowel($arr1[$i+2]) && isVowel($arr1[$i+3]))
{
$updatedString = substr_replace($string, "-", $i+1, 0);
return $updatedString;
}
}
else
{
if(isVowel($arr1[$i]) && !isVowel($arr1[$i+1]) && !isVowel($arr1[$i+2]) && isVowel($arr1[$i+3]))
{
$updatedString = substr_replace($string, "-", $i+2, 0);
return $updatedString;
}
}
}
}
$st1 = $string;
for($k=0;$k<$pc;$k++)
{
$st1 = strAppend2($st1);
}
return $st1;
}
function xRule($string)
{
return preg_replace('/([aeiou]x)([aeiou])/', '$1-$2', $string);
}
function isVowel($ch)
{
if($ch=='a' || $ch=='e' || $ch=='i' || $ch=='o' || $ch=='u')
{
return true;
}
else
{
return false;
}
}
function isConsonant($ch)
{
if($ch=='a' || $ch=='e' || $ch=='i' || $ch=='o' || $ch=='u')
{
return false;
}
else
{
return true;
}
}
?>
私はこれらの機能をすべて組み合わせれば、望ましい出力が得られると思います。しかし、私は下の私の制約を指定します:あなたは、各ルールを注意深く見ている場合
Rule 1 : When two or more consonants are between vowels, the first consonant is joined to the preceding vowel; for example - rec-tor, trac-tor, ac-tor, delec-tus, dic-tator, defec-tus, vic-tima, Oc-tober, fac-tum, pac-tus,
Rule 2 : 'x' is joined to the preceding vowel; as, rex-i.
However we give a special exception to the following consonants - br, cr, dr, fr, gr, pr, tr; bl, cl, fl, gl, pl, phl, phr, ch, ph, th, qu. These consonants are taken care by adding them to the later vowel for example - con- sola-trix
n- sola-trix.
Rule 3 : When 'ct' follows a consonant, that consonant and 'c' are both joined to the first vowel for example - sanc-tus and junc-tum
Similarly for 'pt' we apply the same rule for example - scalp-tum, serp-tum, Redemp-tor.
Rule 4 : A single consonant between two vowels is joined to the following vowel for example - ma-ter, pa-ter AND Z is joined to the following vowel.
Rule 5 : When two vowels come together they are divided, if they be not a diphthong; as au-re-us. Diaphthongs are - "ae","oe","au"
シセロはそれに同意しますか? –
複数の正規表現または複数の関数を組み合わせる前に、各関数が何をするかを最初に判断する必要があります。例えば、関数twoVowelsは二重音を考慮しない。 –
'strAppend2'は何をする予定ですか?あなたのコードにコメントをつけ、それぞれの関数の簡単な説明を追加する必要があります(phpDocについての検索)。 –