本文介绍了mb_strtolower不适用于法语元音é的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个PHP函数,当 $ word_two 以元音开头时,该函数删除了 $ word_one 的最后一个字母,并给该单词加上撇号.但是对于法语元音é来说,它不起作用,尽管我将 mb_strtolower 'UTF-8'一起使用.

I have a PHP function, which delete the last letter of $word_one and put an apostroph to this word, when the $word_two begins with a vowel. But with the french vowel é it doesn't work, although I use the mb_strtolower with , 'UTF-8'.

我不想更改 $ pers 数组,因为在 $ word_two 不是以元音开头的单词中,我也有一些组合.>

I don't want to change the $pers array, because I have also combination for words where the $word_two doesn't begin with a vowel.

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]).'';

推荐答案

preg 函数在unicode模式( u )中通常比 mb_xxx :

preg functions in unicode mode (u) are usually easier to use than 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 je 任何je :

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

这篇关于mb_strtolower不适用于法语元音é的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 14:37