One of the things that irks me most about programmers getting involved with marketing is that most of them just don't know grammar. SEO Black Hat mentioned "Google are," I mentioned "then vs. than," and now I'll mention one more thing -- "a vs. an." This one is a little different. Most people know this rule intuitively. But programmers frequently get lazy and do not look programmatically at the following word to decide which indefinite article to use. Being a programmer makes me slightly more forgiving, but this still looks bad. After I release this code, there are no more excuses:
<?
function aOrAn($next_word)
{
$_an = array('hour', 'honest', 'heir', 'heirloom');
$_a = array('use', 'useless', 'user');
$_vowels = array('a','e','i','o','u');
$_endings = array('ly', 'ness', 'less', 'lessly', 'ing', 'ally', 'ially');
$_endings_regex = implode('|', $_endings);
$tmp = preg_match('#(.*?)(-| |$)#', $next_word, $captures);
$the_word = trim($captures[1]);
//$the_word = Format::trimString(Utils::pregGet('#(.*?)(-| |$)#', $next_word, 1));
$_an_regex = implode('|', $_an);
if (preg_match("#($_an_regex)($_endings_regex)#i", $the_word)) {
return 'an';
}
$_a_regex = implode('|', $_a);
if (preg_match("#($_a_regex)($_endings_regex)#i", $the_word)) {
return 'a';
}
if (in_array(strtolower($the_word{0}), $_vowels)) {
return 'an';
}
return 'a';
}
?>
This code may not be bulletproof, but it nails most of the major rules and exceptions. So if I see one more lazy PHP programmer making this stupid mistake, I'm just closing the window, OK? :)












September 16th, 2006 at 11:35 am
That is great stuff.
--G
September 26th, 2006 at 5:45 pm
Some of the a/an rules are a bit more complex and can't be embedded in code because they depend on how you pronounce words. For example its "a hotel" and "an 'otel" if you drop your aitches.
Btw, can you provide code to help programmers with the its/it's problems too?
Cheers,
Adrian