PHP-php 如何实现 英语单词 单复数 转换

PHP-php 如何实现 英语单词 单复数 转换

浮生未歇 发布于 2016-12-15 字数 39 浏览 1248 回复 2

php如何实现 英语单词单复数的转换

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浮生未歇 2017-01-21 2 楼

老外已经写好了,要看他的nb大作就点这里吧,下面是效果:

/* Singular to plural / Plural to singular */
echo Inflector::pluralize('search'); // outputs searches
echo Inflector::singularize('cases'); // outputs case
echo Inflector::pluralize('query'); // outputs queries
echo Inflector::singularize('queries'); // outputs query
echo Inflector::pluralize('ability'); // outputs abilities
echo Inflector::singularize('abilities'); // outputs ability
echo Inflector::pluralize('analysis'); // outputs analyses
echo Inflector::singularize('analyses'); // outputs analysis
echo Inflector::pluralize('information'); // outputs information
echo Inflector::singularize('information'); // outputs information
echo Inflector::pluralize('mouse'); // outputs mice
echo Inflector::singularize('mice'); // outputs mouse

清晨说ぺ晚安 2016-12-15 1 楼

提供一个单数转复数的函数给你:

/**
* 单词单数转成复数
* @param $string 单词单数
*/
function pluralize( $string ) {
$plural = array(
array( '/(quiz)$/i', "$1zes" ),
array( '/^(ox)$/i', "$1en" ),
array( '/([m|l])ouse$/i', "$1ice" ),
array( '/(matr|vert|ind)ix|ex$/i',"$1ices" ),
array( '/(x|ch|ss|sh)$/i', "$1es" ),
array( '/([^aeiouy]|qu)y$/i', "$1ies" ),
array( '/([^aeiouy]|qu)ies$/i', "$1y" ),
array( '/(hive)$/i', "$1s" ),
array( '/(?:([^f])fe|([lr])f)$/i',"$1$2ves" ),
array( '/sis$/i', "ses" ),
array( '/([ti])um$/i', "$1a" ),
array( '/(buffal|tomat)o$/i', "$1oes" ),
array( '/(bu)s$/i', "$1ses" ),
array( '/(alias|status)$/i', "$1es" ),
array( '/(octop|vir)us$/i', "$1i" ),
array( '/(ax|test)is$/i', "$1es" ),
array( '/s$/i', "s" ),
array( '/$/', "s" )
);

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

$irregular = array(
array( 'move', 'moves' ),
array( 'sex', 'sexes' ),
array( 'child', 'children' ),
array( 'man', 'men' ),
array( 'person', 'people' )
);

$uncountable = array(
'sheep',
'fish',
'series',
'species',
'money',
'rice',
'information',
'equipment'
);

if ( in_array( strtolower( $string ), $uncountable ) ) return $string;

foreach ( $irregular as $noun ){
if ( strtolower( $string ) == $noun[0] )
return $noun[1];
}

foreach ( $plural as $pattern ){
if ( preg_match( $pattern[0], $string ) )
return preg_replace( $pattern[0], $pattern[1], $string );
}
$string;
}

echo pluralize("book");