# Indefinite article a/an [closed]

I'm looking for feedback to improve this function. The purpose is to pass a noun and append the appropriate indefinite article ("a" or "an").

A known issue with this code is that it doesn't address words with a vowel sound but a consonant first letter, such as "honor" or "hour". I cannot include 'h' in the list of vowels, though; words like "home" or "horror" have a harder 'h' sound and use the article "a".

function aan ($string) { return ( in_array( strtolower(substr($string, 0, 1)),
array('a','e','i','o','u')
) ? 'an' : 'a'
).' '.$string; } Current output: print 'Become '.aan('All-access').' member'; // Become an All-access member print 'Become '.aan('Basic').' member'; // Become a Basic member print 'It has been '.aan('interesting').' experience'; // It has been an interesting experience - ## closed as off-topic by Malachi, syb0rg, Jeff Vanzella, Jamal♦Dec 21 '13 at 15:47 If this question can be reworded to fit the rules in the help center, please edit the question. The logic is flawed. In English, you can't base a/an on written form. You have to base it on word pronuncation. If you don't have the pronunciation for every word, you will never make it work. Common examples - "a unit" vs "an uncle" – Sulthan Jan 8 '13 at 16:23 @Sulthan ... as I mention in the second paragraph. – Chris Mar 4 '13 at 15:05 it is unclear what kind of feedback you are looking for to improve this function. performance, readability, speed, efficiency.... – Malachi Dec 21 '13 at 3:56 ## 2 Answers It would be simpler to use stristr() and$string[0].

function indefinite_article_heuristic($string) { return (stristr('aeiou',$string[0]) ? 'an ' : 'a ') . $string; } stripos() would probably be slightly more efficient, but you would have to do function indefinite_article_heuristic($string) {
return (FALSE === stripos('aeiou', $string[0]) ? 'a ' : 'an ') .$string;
}

which looks icky.

-

A tiny improvement to reduce a string concatenation:

function aan ($string) { return ( in_array( strtolower(substr($string, 0, 1)),
array('a','e','i','o','u')
) ? 'an ' : 'a '
).\$string;
}

In order to help get your logic for more cases (ie, your Honor or Hour examples), you may wish to visit the English StackExchange to get help in figuring that logic out. Then you can code that as well.

-