1.[代码]参考ROR中的单复数转换,写一个PHP的单复数转换类$inflector = new Inflector();

echo $inflector->pluralize('bus') . '
';

echo $inflector->singularize('buses') . '
';

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('/$/', 's');

$this->_plural('/s$/i', 's');

$this->_plural('/(ax|test)is$/i', '\1es');

$this->_plural('/(octop|vir)us$/i', '\1i');

$this->_plural('/(octop|vir)i$/i', '\1i');

$this->_plural('/(alias|status)$/i', '\1es');

$this->_plural('/(bu)s$/i', '\1ses');

$this->_plural('/(buffal|tomat)o$/i', '\1oes');

$this->_plural('/([ti])um$/i', '\1a');

$this->_plural('/([ti])a$/i', '\1a');

$this->_plural('/sis$/i', 'ses');

$this->_plural('/(?:([^f])fe|([lr])f)$/i', '\1\2ves');

$this->_plural('/(hive)$/i', '\1s');

$this->_plural('/([^aeiouy]|qu)y$/i', '\1ies');

$this->_plural('/(x|ch|ss|sh)$/i', '\1es');

$this->_plural('/(matr|vert|ind)(?:ix|ex)$/i', '\1ices');

$this->_plural('/(m|l)ouse$/i', '\1ice');

$this->_plural('/(m|l)ice$/i', '\1ice');

$this->_plural('/^(ox)$/i', '\1en');

$this->_plural('/^(oxen)$/i', '\1');

$this->_plural('/(quiz)$/i', '\1zes');

}

private function _update_singular() {

$this->_singular('/s$/i', '');

$this->_singular('/(n)ews$/i', '\1ews');

$this->_singular('/([ti])a$/i', '\1um');

$this->_singular('/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i', '\1\2sis');

$this->_singular('/(^analy)ses$/i', '\1sis');

$this->_singular('/([^f])ves$/i', '\1fe');

$this->_singular('/(hive)s$/i', '\1');

$this->_singular('/(tive)s$/i', '\1');

$this->_singular('/([lr])ves$/i', '\1f');

$this->_singular('/([^aeiouy]|qu)ies$/i', '\1y');

$this->_singular('/(s)eries$/i', '\1eries');

$this->_singular('/(m)ovies$/i', '\1ovie');

$this->_singular('/(x|ch|ss|sh)es$/i', '\1');

$this->_singular('/(m|l)ice$/i', '\1ouse');

$this->_singular('/(bus)es$/i', '\1');

$this->_singular('/(o)es$/i', '\1');

$this->_singular('/(shoe)s$/i', '\1');

$this->_singular('/(cris|ax|test)es$/i', '\1is');

$this->_singular('/(octop|vir)i$/i', '\1us');

$this->_singular('/(alias|status)es$/i', '\1');

$this->_singular('/^(ox)en/i', '\1');

$this->_singular('/(vert|ind)ices$/i', '\1ex');

$this->_singular('/(matr)ices$/i', '\1ix');

$this->_singular('/(quiz)zes$/i', '\1');

$this->_singular('/(database)s$/i', '\1');

}

private function _update_irregular() {

$this->_irregular('person', 'people');

$this->_irregular('man', 'men');

$this->_irregular('child', 'children');

$this->_irregular('sex', 'sexes');

$this->_irregular('move', 'moves');

$this->_irregular('cow', 'kine');

$this->_irregular('zombie', 'zombies');

}

private function _update_uncountable() {

$this->_uncountable('equipment');

$this->_uncountable('information');

$this->_uncountable('rice');

$this->_uncountable('money');

$this->_uncountable('species');

$this->_uncountable('series');

$this->_uncountable('fish');

$this->_uncountable('sheep');

$this->_uncountable('jeans');

}

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('/(' . substr($singular, 0, 1) . ')' . substr($singular, 1) . '$/i', '\1' . substr($plural, 1));

$this->_plural('/(' . substr($plural, 0, 1) . ')' . substr($plural, 1) . '$/i', '\1' . substr($plural, 1));

$this->_singular('/(' . substr($plural, 0, 1) . ')' . substr($plural, 1) . '$/i', '\1' . substr($singular, 1));

} else {

$this->_plural('/' . strtoupper(substr($singular, 0, 1)) . '(?i)' . substr($singular, 1) . '$/',

strtoupper(substr($plural, 0, 1)) . substr($plural, 1));

$this->_plural('/' . strtolower(substr($singular, 0, 1)) . '(?i)' . substr($singular, 1) . '$/',

strtolower(substr($plural, 0, 1)) . substr($plural, 1));

$this->_plural('/' . strtoupper(substr($plural, 0, 1)) . '(?i)' . substr($plural, 1) . '$/',

strtoupper(substr($plural, 0, 1)) . substr($plural, 1));

$this->_plural('/' . strtolower(substr($plural, 0, 1)) . '(?i)' . substr($plural, 1) . '$/',

strtolower(substr($plural, 0, 1)) . substr($plural, 1));

$this->_singular('/' . strtoupper(substr($plural, 0, 1)) . '(?i)' . substr($plural, 1) . '$/',

strtoupper(substr($singular, 0, 1)) . substr($singular, 1));

$this->_singular('/' . strtolower(substr($plural, 0, 1)) . '(?i)' . substr($plural, 1) . '$/',

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 */

相关标签:

本文原创发布php中文网,转载请注明出处,感谢您的尊重!

php 日期单数 复数,参考ROR中的单复数转换,写一个PHP的单复数转换类相关推荐

  1. Qt 5.9.1 参考手册 QtTest 第5章 写一个基准线

    Qt 5.9.1 Reference Documentation Chapter 5: Writing a Benchmark In this final chapter we will demons ...

  2. 单数复数php单元格背景颜色,PHP-php 如何实现 英语单词 单复数 转换

    提供一个单数转复数的函数给你: /** * 单词单数转成复数 * @param $string 单词单数 */ function pluralize( $string ) { $plural = ar ...

  3. 在python中使用linux命令写一个监控脚本

    编写python监控脚本,监控/和/boot分区的使用率,/ 大于60%就告警,在屏幕上输出内容,具体自己定义 /boot分区大于50%就告警 脚本名monitor_partition.py 将磁盘的 ...

  4. c语言中如何将字符串转成16进制,用c语言写一个函数把十进制转换成十六进制,该如何处理...

    用c语言写一个函数把十进制转换成十六进制 用c语言写一个函数把十进制转换成十六进制 网上找到一些,感觉有占乱 分享到: ------解决方案-------------------- #include ...

  5. 6 日期字符串转日期_Java日期时间API系列6-----Jdk8中java.time包中的新的日期时间API类...

    因为Jdk7及以前的日期时间类的不方便使用问题和线程安全问题等问题,2005年,Stephen Colebourne创建了Joda-Time库,作为替代的日期和时间API.Stephen向JCP提交了 ...

  6. 数据库表命名 单数复数_数据是还是数据是? “数据”一词是单数还是复数?

    数据库表命名 单数复数 I'll cut right to the chase: the word "data" is plural. It's the plural form o ...

  7. java 包结构 枚举类_Java日期时间API系列6-----Jdk8中java.time包中的新的日期时间API类...

    因为Jdk7及以前的日期时间类的不方便使用问题和线程安全问题等问题,2005年,Stephen Colebourne创建了Joda-Time库,作为替代的日期和时间API.Stephen向JCP提交了 ...

  8. php 中日期时间函数大全,PHP 中日期时间函数 date() 用法总结

    [导读] date()是我们常用的一个日期时间函数,下面我来总结一下关于date()函数的各种形式的用法,有需要学习的朋友可参考.格式化日期date() 函数的第一个参数规定了如何格式化日期 时间.它 ...

  9. 【Vue】基础(三)条件渲染 - 列表渲染(key的作用与原理虚拟DOM解析) - 收集表单数据 - 持续更新中

    目录 11. 条件渲染 11.1 v-if 11.2 v-show 12. 列表渲染 12.1 v-for(基本列表使用) 12.2 key的作用与原理 真实DOM和其解析流程 虚拟 DOM 的好处 ...

最新文章

  1. 哈勃望远镜进一步确认宇宙在加速膨胀
  2. 老板说 10 分钟可改完 Bug,为什么我却干了 3 小时?
  3. javaScript 之 蚁人微任务
  4. 转载:linux+arm 网卡故障调试:ethtoolphy寄存器读写
  5. 程序员避免颈椎病攻略
  6. hbase的集群搭建
  7. 原python基础概念整理_Python从头学之基础概念整理
  8. log4net 日志框架的配置
  9. xtrabackup之Innobackupex全备数据库
  10. php 表单数据的获取代码,php 表单数据的获取代码
  11. c语言跑马灯循环三次停止,跑马灯代码 连续不间断的跑马灯的代码(js)
  12. Microchip PIC系列8位单片机入门教程(七)PWM
  13. 【LeetCode】18. 4Sum 四数之和
  14. linux( sudo bmon ) 流量监控工具----类似于 moniter interface
  15. 域名实名认证中的常见问题
  16. maximo开发经验
  17. MFC 视图-OpenGL场景-CDialogBar三个界面保存为图片
  18. 详解clickhouse分区目录的合并过程
  19. 云流量成为数据中心的王者 五年内全球超大规模IDC将不断出现
  20. DLT645协议解析(二)---07协议数据帧结构解析

热门文章

  1. Mariadb互为主从(双主模式)配置
  2. 深入探讨三角函数的命名规范
  3. 归一化的好处及归一化,标准化的处理方法
  4. wpsup计算机内存不足处理方法,开几个wps就内存不足,电脑弹出内存不足-
  5. 谷歌浏览器不显示网站中的部分图片
  6. 2022年牛客网最热门爆火Java岗面试八股文汇总,“吃透”涨薪15k没问题
  7. C++ 内存泄漏检测与实现
  8. 彻底搞懂计算机计算补码,就像接近宇宙的真理
  9. 服务器文件权限在哪里设置密码,共享服务器文件权限怎么设置密码
  10. 智慧农业·智能灌溉系统