2010年5月24日 星期一

自訂Function

  • 亂數排序
<cffunction name="fun_rand" returntype="String" output="false">
<cfargument name="num" type="numeric" required="true">
<cfset old_list = "">
<cfloop condition="#listlen(old_list)# lt #num#">
<cfset R_num=randrange(1,num)>
<cfif ListFind(old_list,R_num) lte 0>
<cfset old_list=listappend(old_list,"#R_num#")>
</cfif>
</cfloop>
<cfreturn old_list >
</cffunction>

使用方法:<cfset rand_list = fun_rand(8)> rand_list="3,5,1,7,4,2,8,6"

  • 亂數list num:亂數最大值 list_len:取回list長度
<cffunction name="fun_rand_len" returntype="String" output="false">
<cfargument name="num" type="numeric" required="true">
<cfargument name="list_len" type="numeric" required="true">
<cfif list_len gt num >
<cfset list_len = num>
</cfif>
<cfset old_list = "">
<cfloop condition="#listlen(old_list)# lt #list_len#">
<cfset R_num=randrange(1,num)>
<cfif ListFind(old_list,R_num) lte 0>
<cfset old_list=listappend(old_list,"#R_num#")>
</cfif>
</cfloop>
<cfreturn old_list >
</cffunction>
使用方法:<cfset rand_list = fun_rand(8,4)> rand_list="3,5,1,7"

  •  截字串長度
<cffunction name="fun_str" returntype="String" output="false">
<cfargument name="value" type="String" required="true">
<cfargument name="max1" type="numeric" required="true">
<cfif len(value) gt max1>
<cfreturn left(value,max1) &"..." >
<cfelse>
<cfreturn value >
</cfif>
</cffunction>

使用方法:<cfset str_list = fun_str("abcdefg",4)> str_list="abcd..."

  • 將 特殊字(ex:&#22531) 轉成 中文字 (ex:堃)
<cffunction name="U8toAsc" output="no">
<cfargument name="InputStr" type="string" required="true">
<cfset specWordList = "">
<cfset findPos = 0 >
<cfset findAry = refind( "&##[0-9]{3,};", InputStr ,findPos ,"true" ) >
<cfset findPos = findAry.pos[1]>
<cfloop condition="findPos gt 0">
<cfset specWordList = listappend(specWordList,mid(InputStr,findAry.pos[1],findAry.len[1]))>
<cfset findAry = refind( "&##[0-9]{3,};", InputStr ,findPos + 1 ,"true" ) >
<cfset findPos = findAry.pos[1]>
</cfloop>
<cfloop list="#specWordList#" index="specWord">
<cfset utf8Asc = replace(replace(specWord,"&##","","all"),";","","all")>
<cfset InputStr = replace(InputStr,specWord,chr(utf8Asc),"all")>
</cfloop>
<cfreturn InputStr>
</cffunction>
使用方法:<cfset str_list =U8toAsc("&##22531;")> str_list=堃

沒有留言:

張貼留言