Car*_*olm -2 php detect multibyte
$word = "Acrobat" (or Apple, Tea etc.)
Run Code Online (Sandbox Code Playgroud)
如何用php检测并回显给定单词的最后一个元音?我尝试了preg_match功能,google了几个小时但找不到合适的解决方案.
字符串中可以有多字节字母,如ü,ö.
这是一个捕获字符串中最后一个元音的多字节安全版本.
$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;
}
Run Code Online (Sandbox Code Playgroud)
这是输出.
在过去的元音"苹果"是ê
去年元音"茶"是一个
有中没有元音"Strng".
在过去的元音"排队"是ê
去年元音"asartä"是ä
最后的元音在"无"是Ø
在最后元音"AGIR"是ı
最后元音"诺埃尔"是Ë
最后元音"GOR"是Ø
最后元音在"æsc"是æ
| 归档时间: |
|
| 查看次数: |
2130 次 |
| 最近记录: |