Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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
share|improve this question

closed as off-topic by Malachi, syb0rg, Jeff Vanzella, Jamal Dec 21 '13 at 15:47

  • This question does not appear to be a code review request within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

3  
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
2  
@Sulthan ... as I mention in the second paragraph. –  Chris Mar 4 '13 at 15:05
3  
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
add comment

2 Answers

up vote 1 down vote accepted

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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.