How to detect and echo the last vowel in a word?

好久不见. 提交于 2019-12-19 05:10:41

问题


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

How can I detect and echo the last vowel of a given word with php? I tried preg_match function, google'd for hours but couldn't find a proper solution.

There can be multibyte letters like ü, ö in the string.


回答1:


Here's a multibyte safe version of catching the last vowel in a string.

$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;    
}

Here's the output.

last vowel in "Apple" is e
last vowel in "Tea" is a
there's no vowel in "Strng".
last vowel in "queue" is e
last vowel in "asartä" is ä
last vowel in "nő" is ő
last vowel in "ağır" is ı
last vowel in "NOËL" is Ë
last vowel in "gør" is ø
last vowel in "æsc" is æ




回答2:


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



回答3:


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");



回答4:


use the method str_split()

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

this will transform your string into an array then you can use the index of the array in finding the first and last letter of your string.

-Array[0] is the first letter *Array[total number of letters - 1] is the last letter.

After getting the first and last elements in the array, the mapping begins. eg: if(letter=="a"||letter=="A") echo "Apple";

and so on...




回答5:


I would say Ed or Mark's answers are definitely more efficient, but if you want an example of a recursive function here it is:

$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);

Mind you, there are better ways to do this. Just giving a specific example, not necessarily the "best" one.




回答6:


I would personally use preg_match() for this task.

$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;   
}

This will output

last vowel in "Apple" is e
last vowel in "Tea" is a
there's no vowel in "Strng".
last vowel in "queue" is e



来源:https://stackoverflow.com/questions/14268970/how-to-detect-and-echo-the-last-vowel-in-a-word

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!