Vivid Lust Posted January 6, 2010 Share Posted January 6, 2010 Does anyone know of a function that can look at a word an determine if an or a should go before it. Example: INPUT: english OUTPUT: an INPUT: spanish OUTPUT: a Thanks all for any help, Jake Quote Link to comment Share on other sites More sharing options...
trq Posted January 6, 2010 Share Posted January 6, 2010 You'll need to be more descriptive. Your example makes little sense to me. Quote Link to comment Share on other sites More sharing options...
salathe Posted January 6, 2010 Share Posted January 6, 2010 thorpe, the OP wants to be able to determine whether "an" or "a" should preceed a word. For example given the words apple, banana, orange the correct choices would be an, a, an respectively. Quote Link to comment Share on other sites More sharing options...
Catfish Posted January 6, 2010 Share Posted January 6, 2010 a regular expression preg_match() looking for the string starting with a vowel as a condition inside an if() statement would provide flow for "an" and an else {} for any other result ("a") would probably do it. don't have time to work out the regexp to use but. Quote Link to comment Share on other sites More sharing options...
trq Posted January 6, 2010 Share Posted January 6, 2010 I'm not sure how that would be achieved through php. The rule is that you use a before words that start with a consonant sound and an before words that start with a vowel sound. Quote Link to comment Share on other sites More sharing options...
Catfish Posted January 6, 2010 Share Posted January 6, 2010 yeah i was wondering about how many times it would actually fail if you just based it on the starting letter, but couldn't think of many words where it doesn't, though knowing english, there is probably many of them. Quote Link to comment Share on other sites More sharing options...
Vivid Lust Posted January 6, 2010 Author Share Posted January 6, 2010 I'm looking for one that just checks the starting letter, thanks. Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted January 6, 2010 Share Posted January 6, 2010 if your looking for one that checks the starting letter I think the following should do <?php function getBefore($word) { return in_array(strtolower(substr($word,0,1)), array('a','e','i','o','u')) ? 'an' : 'a'; } echo getBefore('english') . '<br />'; echo getBefore('spanish') . '<br />'; ?> Quote Link to comment Share on other sites More sharing options...
salathe Posted January 6, 2010 Share Posted January 6, 2010 You could also replace the return statement in rajivgonsalves' function with the similarly functioning: return strspn(strtolower($word[0]), 'aeiou') ? 'an' : 'a'; Quote Link to comment Share on other sites More sharing options...
MoonParsnip Posted January 5, 2016 Share Posted January 5, 2016 A bit of a late reply... however hopefully this is useful for anyone stumbling across this forum post: http://www.technologywales.co.uk/coding-solution-using-an-or-a-before-a-word/ 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.