我有一个函数,它应该创建许多法语动词的变化表。
当french_personal_pronouns[0]后面的下一个值开始时,第一个字母是元音(a,e, I,o,u),我如何始终将$french_personal_pronouns的第一个值从"je"更改为"j"?
<?php
$french_personal_pronouns = array("je", "tu", "il", "nous", "vous", "ils");
$aimer = array ("aime", "aimes", "aime", "aimons", "aimez", "aiment");
$dire = array ("dis", "dit", "disons", "dites", "disent");
echo "$french_personal_pronouns[0] $aimer[1]"."<br>"; // result je aime -> should be j'aime
echo "$french_personal_pronouns[0] $dire[1]"."<br>";
?>
如何循环创建适当的应用程序结构取决于您,但在您的特定示例中,您可以在输出下一个值之前查找下一个值,并将其与元音数组进行比较。
我写了一个简单的元音检查函数,并将比较包在另一个函数中以方便阅读。这里也有一个演示:
// Determine if a letter is a vowel
// @return bool true/false
function isVowel($letter) {
return in_array(strtolower($letter), array('a', 'e', 'i', 'o', 'u'));
}
function outputFrench($word_one, $word_two) {
// if the first letter of $word_two is a vowel...
if(isVowel($word_two{0}))
// use the first letter of $word_one with an apostrophe and all of $word_two
$output = $word_one{0} . '''' . $word_two;
else
// other wise combine the words with a space in between
$output = $word_one . ' ' . $word_two;
return $output;
}
echo outputFrench($french_personal_pronouns[0], $aimer[0]); // j'aime