mb_strtolower不适用于法语元音é

时间:2014-08-31 17:24:34

标签: php function internationalization

我有一个PHP函数,它删除$word_one的最后一个字母,当$word_two以元音开头时,将一个叛逆者放到这个单词中。但是使用法语元音é它不起作用,尽管我使用mb_strtolower, 'UTF-8'

我不想更改$pers数组,因为我还有$word_two不以元音开头的单词的组合。

function French($word_one, $word_two) {
    if (in_array(mb_strtolower($word_two{0}, 'UTF-8'), array('a', 'e', 'é', 'i', 'o')) and ($word_one == "je" or $word_one == "que je"))    
        // delete last letter in $word_one and add an apostrophe and all of $word_two  é don't work     
        $output = substr($word_one, 0, -1) . '\'' . $word_two;        
    else
        // other wise combine the words with a space in between
        $output = $word_one . ' ' . $word_two;  
    return $output;
}

示例:

$pers = array('je', "tu", "il/elle/on", "nous", "vous", "ils/elles");
$que_pers = array("que je", "que tu", "qu'il/elle/on", "que nous", "que vous", "qu'ils/elles");
$Ind['I']   = array ('étais','étais','était','étions','étiez','étaient');
$Sub['Pré'] = array ('aie','aies','ait','ayons','ayez','aient');

echo ''.French($pers[0], $Ind['I'][0]).'';
echo ''.French($que_pers[0], $Sub['Pré'][0]).'';

1 个答案:

答案 0 :(得分:3)

unicode模式下的

preg函数(u)通常比mb_xxx更容易使用:

function French($word_one, $word_two) {
    if($word_one == 'je' && preg_match('~^[aeéio]~ui', $word_two))
        return "j'$word_two";
    return "$word_one $word_two";
}

还要匹配que jewhatever je

if(preg_match('~(.*)\bje$~ui', $word_one, $m) && preg_match('~^[aeéio]~ui', $word_two))
    return "{$m[1]}j'$word_two";