php如何实现 英语单词单复数的转换
老外已经写好了,要看他的nb大作就点这里吧,下面是效果:
/* Singular to plural / Plural to singular */echo Inflector::pluralize('search'); // outputs searchesecho Inflector::singularize('cases'); // outputs caseecho Inflector::pluralize('query'); // outputs queriesecho Inflector::singularize('queries'); // outputs queryecho Inflector::pluralize('ability'); // outputs abilitiesecho Inflector::singularize('abilities'); // outputs abilityecho Inflector::pluralize('analysis'); // outputs analysesecho Inflector::singularize('analyses'); // outputs analysisecho Inflector::pluralize('information'); // outputs informationecho Inflector::singularize('information'); // outputs informationecho Inflector::pluralize('mouse'); // outputs miceecho Inflector::singularize('mice'); // outputs mouse
提供一个单数转复数的函数给你:
/*** 单词单数转成复数* @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");
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
暂无简介
文章 0 评论 0
接受
发布评论
评论(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
提供一个单数转复数的函数给你:
/**
* 单词单数转成复数
* @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");