开发者

Correct form of indefinite article (a, an) in PHP strings

Is there an easy way to substitute a/an in a string to agree with the following word - much the same as the way 'S' works in Date format?

e.g.

$appl开发者_如何学JAVAe = 'apple';
$pear = 'pear';
echo "This is a $apple, this is a $pear."

--> This is an apple, this is a pear


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

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


You could use a regular expression to swap the a/and depending on what follows it. The trickier part will actually be defining all of the cases on which to swap - it is more complicated then 'if its followed by a vowel'.

When to use a/an:

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

Hence:

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

Beginning of a regex to solve it

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


Try this :

$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";
}

output:

an apple is a fruit
a banana is also a fruit


not sure if it works in PHP that 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 as there is no such rule in german and i usually use the one that sounds better)

Explanation:

\b is a word boundary, so \ba\b looks 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 with an followed by the captured letter.


I've forked the module Luke Chavers refers to, cleaning it up, fixing a logic error and making it integrable using Composer; with that 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.


I used Luke Chaver's answer to write a quick and nasty php snippet to do 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][A-Z]
        ";
$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("/^[a-z][.-]/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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜