如何检测和回显单词中的最后一个元音?

时间:2013-01-10 23:10:48

标签: php detect multibyte

$word = "Acrobat" (or Apple, Tea etc.)

如何使用php检测并回显给定单词的最后一个元音?我尝试了preg_match功能,google了几个小时但找不到合适的解决方案。

字符串中可以有多字节字母,如ü,ö。

6 个答案:

答案 0 :(得分:5)

这是捕获字符串中最后一个元音的多字节安全版本。

$arr = array(
    'Apple','Tea','Strng','queue',
    'asartä','nő','ağır','NOËL','gør','æsc'
);

/*  these are the ones I found in character viewer
    in Mac so these vowels can be extended. don't
    forget to add both lower and upper case versions
    of new ones because personally I wouldn't rely
    on the i (case insensitive) flag in the pattern
    for multibyte characters.
*/
$vowels =
    'aàáâãāăȧäảåǎȁąạḁẚầấẫẩằắẵẳǡǟǻậặæǽǣ' .
    'AÀÁÂÃĀĂȦÄẢÅǍȀȂĄẠḀẦẤẪẨẰẮẴẲǠǞǺẬẶÆǼǢ' .
    'EÈÉÊẼĒĔĖËẺĚȄȆẸȨĘḘḚỀẾỄỂḔḖỆḜ' .
    'eèéêẽēĕėëẻěȅȇẹȩęḙḛềếễểḕḗệḝ' .
    'IÌÍÎĨĪĬİÏỈǏỊĮȈȊḬḮ' .
    'iìíîĩīĭıïỉǐịįȉȋḭḯ' .
    'OÒÓÔÕŌŎȮÖỎŐǑȌȎƠǪỌØỒỐỖỔȰȪȬṌṐṒỜỚỠỞỢǬỘǾŒ' .
    'oòóôõōŏȯöỏőǒȍȏơǫọøồốỗổȱȫȭṍṏṑṓờớỡởợǭộǿœ' .
    'UÙÚÛŨŪŬÜỦŮŰǓȔȖƯỤṲŲṶṴṸṺǛǗǕǙỪỨỮỬỰ' .
    'uùúûũūŭüủůűǔȕȗưụṳųṷṵṹṻǖǜǘǖǚừứữửự'
;

// set necessary encodings
mb_internal_encoding('UTF-8');
mb_regex_encoding('UTF-8');

// and loop
foreach ($arr as $word) {

    $vow = mb_ereg_replace('[^'.$vowels.']','',$word);
    // get rid of all consonants (non-vowels in this pattern)
    $lastVw = mb_substr($vow,-1);
    // and get the last one from the remaining vowels

    if (empty($lastVw))
    // it evaluates this line when there's no vowel in the string
        echo "there's no vowel in <b>\"$word\"</b>." . PHP_EOL;
    else
    // and vice versa
        echo "last vowel in <b>\"$word\"</b> is " .
        "<span style=\"color:#F00\">{$lastVw}</span>" . PHP_EOL;    
}

这是输出。

  

“Apple”中的最后一个元音 e
  “Tea”中的最后一个元音 a
  “Strng”中没有元音。
  “队列”中的最后一个元音 e
  “asartä”中的最后一个元音 ä
  “nő”中的最后一个元音 ő
  “ağır”中的最后一个元音 ı
  “NOËL”中的最后一个元音 Ë
  “gør”中的最后一个元音 ø
  “æsc”中的最后一个元音是 æ

答案 1 :(得分:2)

$word = "Acrobat";
$vowels = array_intersect(str_split($word), array('A','E','I','O','U','a','e','i','o','u'));
echo array_pop($vowels);

答案 2 :(得分:1)

function last_vowel($word) {
  for ($i = strlen($word) - 1; $i >= 0; --$i) {
      switch (strtolower($word[$i])) {
        case 'a': case 'e': case 'i': case 'o': case 'u': case 'y':
          return $word[$i];
      }
  }
  return null;
}

echo last_vowel("Apple");

答案 3 :(得分:0)

使用方法str_split()

http://www.php.net/manual/en/function.str-split.php

这会将您的字符串转换为数组,然后您可以使用数组的索引来查找 你的字符串的第一个和最后一个字母。

-Array [0]是第一个字母 *数组[字母总数 - 1]是最后一个字母。

获取数组中的第一个和最后一个元素后,映射开始。例如:     如果(信== “一” ||信== “A”)      回声“Apple”;

依旧......

答案 4 :(得分:0)

我会说Ed或Mark的答案肯定更有效,但如果你想要一个递归函数的例子,那就是:

$word = "stellar";

function lastVowel($word)
{
    $vowels = array('a','e','i','o','u');
    $letter = substr($word, strlen($word) - 1);
    if(in_array(strtolower($letter), $vowels))
        return $letter;
    else
    {
        if(strlen($word) > 0)
            return lastVowel(substr($word, 0, strlen($word) - 1));
        else
            return "no vowels";
    }
}

echo lastVowel($word);

请注意,有更好的方法可以做到这一点。举一个具体的例子,不一定是“最好的”。

答案 5 :(得分:0)

我个人会使用preg_match()来完成这项任务。

$arr = array(
    'Apple','Tea','Strng','queue'
);

foreach ($arr as $word) {

    preg_match('~[aeiou](?=[^aeiou]*$)~i',$word,$m);

    if (empty($m)) echo "there's no vowel in \"$word\".";
    else echo "last vowel in \"$word\" is <b style=\"color:#F00\">{$m[0]}</b>";
    echo PHP_EOL;   
}

这将输出

  

“Apple”中的最后一个元音是 e
  “茶”中的最后一个元音是 a
  “Strng”中没有元音。
  “队列”中的最后一个元音是 e