The correct form of an undefined article (a, an) in PHP lines

Is there an easy way to substitute a / an in a string to agree with the next word - much in the same way that the "S" method works in Date format?

eg.

$apple = 'apple'; $pear = 'pear'; echo "This is a $apple, this is a $pear." --> This is an apple, this is a pear 
+6
string php cakephp
source share
6 answers

Try the following:

 $l = array('a apple is a fruit', 'a banana is also a fruit'); foreach($l as $s) { $s = preg_replace('/(^| )a ([aeiouAEIOU])/', '$1an $2', $s); echo $s,"\n"; } 

exit:

 an apple is a fruit a banana is also a fruit 
+6
source share

Check it out, it passed my own testing, it seems pretty solid.

https://github.com/Kaivosukeltaja/php-indefinite-article

+7
source share

You can use a regular expression to replace a / and depending on what follows. The more complex part will actually determine all the cases for which a swap is required - this is more complicated than "if it is followed by a vowel."

When to use a / an:

Use words before words / abbreviations that begin with a consonant sound; use before words / abbreviations that start with a vowel. It is based on pronunciation, not spelling.

Consequently:

  • university
  • hour
  • ytterbium molecule
  • yellow dog
  • a U
  • a M

Start regex to solve it

 $text = preg_replace("/(?=a|e|i|o|u|yt)a/", "an", $text); 
+6
source share

not sure if it works in PHP this way, but a really simple solution would be:

 $string = preg_replace('/\ba\b\s([aeiou])/', 'an $1', $string); $string = preg_replace('/\ban\b\s([^aeiou])/', 'an $1', $string); 

(Not sure about the a / an rule, because it doesn't have that rule, and I usually use the one that sounds better)

Explanation:

\ b is the word boundary, so \ ba \ b searches for the word a, followed by a space and one of the letters [aeiou]. The letter is captured to $ 1, and the expression is replaced by an , followed by the captured letter.

+2
source share

I used Luke Chaver to write a quick and nasty piece of PHP to handle this.

 <?php //code inspired by https://github.com/Kaivosukeltaja/php-indefinite-article/blob/master/IndefiniteArticle.class.php global $indef_A_abbrev, $indef_A_y_cons, $indef_A_explicit_an, $indef_A_ordinal_an, $indef_A_ordinal_a; $indef_A_abbrev = "(?! FJO | [HLMNS]Y. | RY[EO] | SQU | ( F[LR]? | [HL] | MN? | N | RH? | S[CHKLMNPTVW]? | X(YL)?) [AEIOU]) [FHLMNRSX][AZ] "; $indef_A_y_cons = 'y(b[lor]|cl[ea]|fere|gg|p[ios]|rou|tt)'; $indef_A_explicit_an = "euler|hour(?!i)|heir|honest|hono"; $indef_A_ordinal_an = "[aefhilmnorsx]-?th"; $indef_A_ordinal_a = "[bcdgjkpqtuvwyz]-?th"; function indefinite_article($input){ global $indef_A_abbrev, $indef_A_y_cons, $indef_A_explicit_an, $indef_A_ordinal_an, $indef_A_ordinal_a; $word = preg_replace("^\s*(.*)\s*^", "$1", $input); if(preg_match("/^[8](\d+)?/", $word)) { return "an $word"; } if(preg_match("/^[1][1](\d+)?/", $word) || (preg_match("/^[1][8](\d+)?/", $word))) { if(strlen(preg_replace(array("/\s/", "/,/", "/\.(\d+)?/"), '', $word))%3 == 2) { return "an $word"; } } if(preg_match("/^(".$indef_A_ordinal_a.")/i", $word)) return "a $word"; if(preg_match("/^(".$indef_A_ordinal_an.")/i", $word)) return "an $word"; if(preg_match("/^(".$indef_A_explicit_an.")/i", $word)) return "an $word"; if(preg_match("/^[aefhilmnorsx]$/i", $word)) return "an $word"; if(preg_match("/^[bcdgjkpqtuvwyz]$/i", $word)) return "a $word"; if(preg_match("/^(".$indef_A_abbrev.")/x", $word)) return "an $word"; if(preg_match("/^[aefhilmnorsx][.-]/i", $word)) return "an $word"; if(preg_match("/^[az][.-]/i", $word)) return "a $word"; if(preg_match("/^[^aeiouy]/i", $word)) return "a $word"; if(preg_match("/^e[uw]/i", $word)) return "a $word"; if(preg_match("/^onc?e\b/i", $word)) return "a $word"; if(preg_match("/^uni([^nmd]|mo)/i", $word)) return "a $word"; if(preg_match("/^ut[th]/i", $word)) return "an $word"; if(preg_match("/^u[bcfhjkqrst][aeiou]/i", $word)) return "a $word"; if(preg_match("/^U[NK][AIEO]?/", $word)) return "a $word"; if(preg_match("/^[aeiou]/i", $word)) return "an $word"; if(preg_match("/^(".$indef_A_y_cons.")/i", $word)) return "an $word"; return "a $word"; } $words = array( "historical", "hour", "wholesale", "administrator", "inner circle" ); foreach ($words as $word) { echo indefinite_article($word); echo "\n"; } ?> 
0
source share

I forked the module referenced by Luke Chaves, cleans it up, fixes a logical error, and makes it integrable with Composer ; with installed, you can pull it into your project with:

 php composer.phar require thaumatic/indefinite-article 

The source code is at https://github.com/thaumatic/indefinite-article .

0
source share

All Articles