I was pretty surprised I couldn’t find this anywhere online, including stackoverflow. I’m looking for the regex to use in the PHP function preg_replace to achieve this:
Find any part of the text where ” a ” is followed by a vowel, and convert the ” a ” to ” an “, following the English grammar rule.
For example:
” a apple” will be converted to ” an apple”.
” a igloo” will be converted to ” an igloo”.
etc. etc.
Ideally, the function would be case-insensitive. What would be really impressive is if it could preserve the original case, e.g. “A” would be converted to “An”, and the case of the next word would be left alone, e.g. “an American” wouldn’t end up as “an american”.
This seems like exactly the sort of thing a regex would be ideal for, and that a lot of people would find useful, but I can’t figure it out. Would be grateful for some help with this.
I’m currently trying this:
preg_replace("/ a?i ([aeio])?i/", "an $1", $string)
As commenters have rightfully pointed out, this won’t completely solve the problem of a / an, as that’s based on sound and not just letters. However, I still think it’s worthwhile on an “every little helps” basis, as it corrects the majority of such issues and so saves some human editing time.
My pattern is as follows, though the issue (re: u) in the comments above is way trickier.
Of course, you could take a gamble if u is the only trouble letter, and trust the source string in the case where u follows, simply remove it from the pattern:
/\b(a)\s+([aeio])/i