2013-05-16 3 views
1

한 단어 문자열에서 첫 번째 모음의 위치를 ​​찾아야합니다. 예를 들어. 첫 번째 모음의 위치를 ​​php의 문자열에서 찾아야합니다.

  • garbage

    1
  • plastic2
  • apple0

내가 초보자이야 반환 반환 반환, 그래서 나는이와 힘든 시간을 보내고 있습니다. 배열이나 정규 표현식으로 루프가 필요하다고 생각합니다.

+0

일 : http://stackoverflow.com/questions/6825506/what-is-the-most-efficient-way-to-detect-and -replace-the-last-vowel-in-a-string http://stackoverflow.com/questions/14268970/how-to-detect-and-echo-the-last-vowel-in-a-word – sachleen

답변

13

사용 strcspn 기능에 문자열이 처음 나타나는 위치를 찾습니다. 지정한 문자 중 처음과 일치하지 않는 문자열의 길이를 반환합니다. 이들의 오프

$pos = strcspn(strtolower($str), "aeiou"); // and sometimes y 
+1

+1이 논리는 –

+0

완전히 이해할 만하고 유용한 종류의 영리한 방법입니다 :) –

0

strpos - 문자열

예컨대 ==

$email = '[email protected]'; 
    $domain = strpos($email, '@'); 
    echo $domain; // prints 4 
+0

당신은 당신입니까? 그가 실제로 물어 본 것은 확실한가? – swapnesh

+0

이 솔루션은 특정 문자의 첫 번째 항목을 찾지 만 처음에는 a, e, i, o 또는 u가 필요합니다. –

1

<?php $mystring="find first vowel in a string";
$vowel=array();
$vowel['0'] = stripos($mystring,'a');
$vowel['1'] = stripos($mystring,'e');
$vowel['2'] = stripos($mystring,'i');
$vowel['3'] = stripos($mystring,'o');
$vowel['4'] = stripos($mystring,'u');
$min=99999;
for($i=0; $i<5;$i++){
if ($vowel[$i] !== false) {
if($min>$vowel[$i]){
$min=$vowel[$i];
}//end if
}//end if
}end for
if($min==99999){
echo "No vowel found.";
}else{
echo "vowel found at position :".$min;
}
?>

관련 문제