Maison > php教程 > PHP源码 > 参考ROR中的单复数转换,写一个PHP的单复数转换类

参考ROR中的单复数转换,写一个PHP的单复数转换类

PHP中文网
Libérer: 2016-05-25 17:10:11
original
1244 Les gens l'ont consulté

1. [代码]参考ROR中的单复数转换,写一个PHP的单复数转换类      

$inflector = new Inflector();

echo $inflector->pluralize(&#39;bus&#39;) . &#39;<br />&#39;;
echo $inflector->singularize(&#39;buses&#39;) . &#39;<br />&#39;;
Copier après la connexion

2. [文件] inflector.php

<?php

class Inflector {
  private $plural      = array();
  private $singular    = array();
  private $irregular   = array();
  private $uncountable = array();

  public function __construct() {
    $this->_update_plural();
    $this->_update_singular();
    $this->_update_irregular();
    $this->_update_uncountable();
  }


  public function pluralize($word) {
    return $this->_apply_inflections($word, $this->plural);
  }

  public function singularize($word) {
    return $this->_apply_inflections($word, $this->singular);
  }

  private function _apply_inflections($word, $rules) {
    $result = $word;
    if (empty($result)) return $result;
    if (sizeof($this->uncountable) > 0) {
      foreach($this->uncountable as $u) {
        if (preg_match("#^{$u}$#", $result)) {
          return $result;
        }
      }
    }

    for($i = (sizeof($rules) - 1); $i >=0; $i--) {
      $rule = $rules[$i];
      if (preg_match($rule[0], $result)) {
        $result = preg_replace($rule[0], $rule[1], $result);
        break;
      }    
    }

    return $result;
  }

  private function _update_plural() {
    $this->_plural(&#39;/$/&#39;, &#39;s&#39;);
    $this->_plural(&#39;/s$/i&#39;, &#39;s&#39;);
    $this->_plural(&#39;/(ax|test)is$/i&#39;, &#39;\1es&#39;);
    $this->_plural(&#39;/(octop|vir)us$/i&#39;, &#39;\1i&#39;);
    $this->_plural(&#39;/(octop|vir)i$/i&#39;, &#39;\1i&#39;);
    $this->_plural(&#39;/(alias|status)$/i&#39;, &#39;\1es&#39;);
    $this->_plural(&#39;/(bu)s$/i&#39;, &#39;\1ses&#39;);
    $this->_plural(&#39;/(buffal|tomat)o$/i&#39;, &#39;\1oes&#39;);
    $this->_plural(&#39;/([ti])um$/i&#39;, &#39;\1a&#39;);
    $this->_plural(&#39;/([ti])a$/i&#39;, &#39;\1a&#39;);
    $this->_plural(&#39;/sis$/i&#39;, &#39;ses&#39;);
    $this->_plural(&#39;/(?:([^f])fe|([lr])f)$/i&#39;, &#39;\1\2ves&#39;);
    $this->_plural(&#39;/(hive)$/i&#39;, &#39;\1s&#39;);
    $this->_plural(&#39;/([^aeiouy]|qu)y$/i&#39;, &#39;\1ies&#39;);
    $this->_plural(&#39;/(x|ch|ss|sh)$/i&#39;, &#39;\1es&#39;);
    $this->_plural(&#39;/(matr|vert|ind)(?:ix|ex)$/i&#39;, &#39;\1ices&#39;);
    $this->_plural(&#39;/(m|l)ouse$/i&#39;, &#39;\1ice&#39;);
    $this->_plural(&#39;/(m|l)ice$/i&#39;, &#39;\1ice&#39;);
    $this->_plural(&#39;/^(ox)$/i&#39;, &#39;\1en&#39;);
    $this->_plural(&#39;/^(oxen)$/i&#39;, &#39;\1&#39;);
    $this->_plural(&#39;/(quiz)$/i&#39;, &#39;\1zes&#39;);
  }

  private function _update_singular() {
    $this->_singular(&#39;/s$/i&#39;, &#39;&#39;);
    $this->_singular(&#39;/(n)ews$/i&#39;, &#39;\1ews&#39;);
    $this->_singular(&#39;/([ti])a$/i&#39;, &#39;\1um&#39;);
    $this->_singular(&#39;/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i&#39;, &#39;\1\2sis&#39;);
    $this->_singular(&#39;/(^analy)ses$/i&#39;, &#39;\1sis&#39;);
    $this->_singular(&#39;/([^f])ves$/i&#39;, &#39;\1fe&#39;);
    $this->_singular(&#39;/(hive)s$/i&#39;, &#39;\1&#39;);
    $this->_singular(&#39;/(tive)s$/i&#39;, &#39;\1&#39;);
    $this->_singular(&#39;/([lr])ves$/i&#39;, &#39;\1f&#39;);
    $this->_singular(&#39;/([^aeiouy]|qu)ies$/i&#39;, &#39;\1y&#39;);
    $this->_singular(&#39;/(s)eries$/i&#39;, &#39;\1eries&#39;);
    $this->_singular(&#39;/(m)ovies$/i&#39;, &#39;\1ovie&#39;);
    $this->_singular(&#39;/(x|ch|ss|sh)es$/i&#39;, &#39;\1&#39;);
    $this->_singular(&#39;/(m|l)ice$/i&#39;, &#39;\1ouse&#39;);
    $this->_singular(&#39;/(bus)es$/i&#39;, &#39;\1&#39;);
    $this->_singular(&#39;/(o)es$/i&#39;, &#39;\1&#39;);
    $this->_singular(&#39;/(shoe)s$/i&#39;, &#39;\1&#39;);
    $this->_singular(&#39;/(cris|ax|test)es$/i&#39;, &#39;\1is&#39;);
    $this->_singular(&#39;/(octop|vir)i$/i&#39;, &#39;\1us&#39;);
    $this->_singular(&#39;/(alias|status)es$/i&#39;, &#39;\1&#39;);
    $this->_singular(&#39;/^(ox)en/i&#39;, &#39;\1&#39;);
    $this->_singular(&#39;/(vert|ind)ices$/i&#39;, &#39;\1ex&#39;);
    $this->_singular(&#39;/(matr)ices$/i&#39;, &#39;\1ix&#39;);
    $this->_singular(&#39;/(quiz)zes$/i&#39;, &#39;\1&#39;);
    $this->_singular(&#39;/(database)s$/i&#39;, &#39;\1&#39;);
  }

  private function _update_irregular() {
    $this->_irregular(&#39;person&#39;, &#39;people&#39;);
    $this->_irregular(&#39;man&#39;, &#39;men&#39;);
    $this->_irregular(&#39;child&#39;, &#39;children&#39;);
    $this->_irregular(&#39;sex&#39;, &#39;sexes&#39;);
    $this->_irregular(&#39;move&#39;, &#39;moves&#39;);
    $this->_irregular(&#39;cow&#39;, &#39;kine&#39;);
    $this->_irregular(&#39;zombie&#39;, &#39;zombies&#39;);
  }

  private function _update_uncountable() {
    $this->_uncountable(&#39;equipment&#39;);
    $this->_uncountable(&#39;information&#39;);
    $this->_uncountable(&#39;rice&#39;);
    $this->_uncountable(&#39;money&#39;);
    $this->_uncountable(&#39;species&#39;);
    $this->_uncountable(&#39;series&#39;);
    $this->_uncountable(&#39;fish&#39;);
    $this->_uncountable(&#39;sheep&#39;);
    $this->_uncountable(&#39;jeans&#39;);
  }

  private function _plural($rule, $replacement) {
    if (is_string($rule)) unset($this->uncountable[$rule]);
    unset($this->uncountable[$replacement]);
    $this->plural[sizeof($this->plural)] = array($rule, $replacement);
  }

  private function _singular($rule, $replacement) {
    if (is_string($rule)) unset($this->uncountable[$rule]);
    unset($this->uncountable[$replacement]);
    $this->singular[sizeof($this->singular)] = array($rule, $replacement);
  }

  private function _irregular($singular, $plural) {
    unset($this->uncountable[$singular]);
    unset($this->uncountable[$plural]);
    if (strtoupper(substr($singular, 0, 1)) == strtoupper(substr($plural, 0, 1))) {
      $this->_plural(&#39;/(&#39; . substr($singular, 0, 1) . &#39;)&#39; . substr($singular, 1) . &#39;$/i&#39;, &#39;\1&#39; . substr($plural, 1));
      $this->_plural(&#39;/(&#39; . substr($plural, 0, 1) . &#39;)&#39; . substr($plural, 1) . &#39;$/i&#39;, &#39;\1&#39; . substr($plural, 1));
      $this->_singular(&#39;/(&#39; . substr($plural, 0, 1) . &#39;)&#39; . substr($plural, 1) . &#39;$/i&#39;, &#39;\1&#39; . substr($singular, 1));
    } else {
      $this->_plural(&#39;/&#39; . strtoupper(substr($singular, 0, 1)) . &#39;(?i)&#39; . substr($singular, 1) . &#39;$/&#39;, 
        strtoupper(substr($plural, 0, 1)) . substr($plural, 1));
      $this->_plural(&#39;/&#39; . strtolower(substr($singular, 0, 1)) . &#39;(?i)&#39; . substr($singular, 1) . &#39;$/&#39;, 
        strtolower(substr($plural, 0, 1)) . substr($plural, 1));
      $this->_plural(&#39;/&#39; . strtoupper(substr($plural, 0, 1)) . &#39;(?i)&#39; . substr($plural, 1) . &#39;$/&#39;, 
        strtoupper(substr($plural, 0, 1)) . substr($plural, 1));
      $this->_plural(&#39;/&#39; . strtolower(substr($plural, 0, 1)) . &#39;(?i)&#39; . substr($plural, 1) . &#39;$/&#39;, 
        strtolower(substr($plural, 0, 1)) . substr($plural, 1));

      $this->_singular(&#39;/&#39; . strtoupper(substr($plural, 0, 1)) . &#39;(?i)&#39; . substr($plural, 1) . &#39;$/&#39;, 
        strtoupper(substr($singular, 0, 1)) . substr($singular, 1));
      $this->_singular(&#39;/&#39; . strtolower(substr($plural, 0, 1)) . &#39;(?i)&#39; . substr($plural, 1) . &#39;$/&#39;, 
        strtolower(substr($singular, 0, 1)) . substr($singular, 1));
    }
  }

  private function _uncountable($word) {
    $this->uncountable[] = $word;
  }
}

/* End of file inflector.php */
/* Location: ./application/libraties/inflector.php */
Copier après la connexion

                               

                   

source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Recommandations populaires
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal