$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.
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...
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:
Mind you, there are better ways to do this. Just giving a specific example, not necessarily the "best" one.
I would personally use
preg_match()
for this task.This will output
Here's a multibyte safe version of catching the last vowel in a string.
Here's the output.