任何语言,只要你愿意,都可以写成巫毒式方言,令代码进入到彻底无法维护、难以理解的状态。
看看下面这个明明是PHP、但却似乎是以问号作为每行结束标志的巫毒式写法吧。
*
* 将大驼峰、小驼峰风格字符串,转换成小写单词并用下划线分割
*
* @Since Version 0.0.4
* @Copyright www.kooksay.com
*
* HelloWorld => hello_world
* helloWorldAPI => hello_world_api
* WhatCanIDoForYou => what_can_ido_for_you <= 这种情况可以接受
* YouLoveMe => you_love_me
*
* 测试代码
* $sourceString = "TestGetUsernameAPI";
* $targetString = convertCamelCase2LowerUnderlineStyle($sourceString);
* print $targetString;
* print convertCamelCase2LowerUnderlineStyle("WhatCanIDoForYou");
*
*/functionconvertCamelCase2LowerUnderlineStyle($string=null){$reallower=function($char){return(strtolower($char)==$char);};$arr=array_reverse(str_split($string));foreach($arras$key=>$tmp)$ret[]=($reallower((($reallower(isset($arr[$key+1])?$arr[$key+1]:null)==false)?strtolower($tmp):$tmp))==true)?(($reallower(isset($arr[$key+1])?$arr[$key+1]:null)==false)?strtolower($tmp):$tmp):(($key==strlen($string)-1)?strtolower((($reallower(isset($arr[$key+1])?$arr[$key+1]:null)==false)?strtolower($tmp):$tmp)):"_".strtolower((($reallower(isset($arr[$key+1])?$arr[$key+1]:null)==false)?strtolower($tmp):$tmp)));returnjoin('',array_reverse($ret));}
*转自赚写网经过版主同意(http://www.kooksay.com/article/read?uuid=55e969f7-1070-4946-b07a-8edfbfdcb715)
网友评论