我想要反转字符串中元音字母的顺序。我已经编写了下面的代码,但结果为空集。
例如 -
"hello",返回 "holle"
"leetcode",返回 "leotcede"
请帮我完成这个任务。
<?php
function reverseVowels($string) {
$result = '';
$string = array();
$vowels = array();
$strlength = count($string);
for ($i=0; $i < $strlength; $i++) {
$orig = $strlength[$i];
$char = strtolower($strlength[$i]);
if ($char === 'a' || $char === 'e' || $char === 'i' || $char === 'o' || $char === 'u') {
array_push($vowels, $orig);
$orig = null;
}
array_push($string, $orig);
}
$new_strlength = count($string);
for ($i=0; $i < $new_strlength; $i++) {
if (!$string[$i]) {
$string[$i] = array_splice($vowels, count($vowels) - 1, 1);
}
}
$result = $string;
return $result;
}
$res = reverseVowels("hello hello");
print_r($res);
//"hello", return "holle"
//"leetcode", return "leotcede"
?>