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

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

May 25, 2016 pm 05:10 PM

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

$inflector = new Inflector();

echo $inflector->pluralize(&#39;bus&#39;) . &#39;<br />&#39;;
echo $inflector->singularize(&#39;buses&#39;) . &#39;<br />&#39;;
Copy after login

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 */
Copy after login

                               

                   

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)