我有这个函数在Coldfusion中生成slugs:
<cffunction name="generateSlug" output="false" returnType="string"> <cfargument name="str"> <cfargument name="spacer" default="-"> <cfset var ret = "" /> <cfset str = lCase(trim(str)) /> <cfset str = reReplace(str,"[àáâãäå]","a","all") /> <cfset str = reReplace(str,"[èéêë]","e","[ìíîï]","i","[òóôö]","o","[ùúûü]","u","[ñ]","n","[^a-z0-9-]","#spacer#","all") /> <cfset ret = reReplace(str,"#spacer#+","all") /> <cfif left(ret,1) eq "#spacer#"> <cfset ret = right(ret,len(ret)-1) /> </cfif> <cfif right(ret,1) eq "#spacer#"> <cfset ret = left(ret,len(ret)-1) /> </cfif> <cfreturn ret /> </cffunction>
然后我用它调用它:
<cfset stringToBeSlugged = "This is a string abcde àáâãäå èéêë ìíîï òóôö ùúûü ñ año ñññññññññññññ" /> <cfset slug = generateSlug(stringToBeSlugged) /> <cfoutput>#slug#</cfoutput>
但这是输出我这个slu ::
这-是-A-字符串ABCDE-A-A-A-A-A-A-E-E-E-E-I-I-I-I-O-O-O-O-U-U-U-U-N-A-NO-N-N-N-N-N-N-N-N-N-N-N-N-N
似乎所有重音字符都被正确替换了,但是这个函数在替换后插入了一个’ – ‘.为什么?
错误在哪里?
PD:我期待这个输出:
this-is-a-string-abcde-aaaaaa-eeee-iiii-oooo-uuuu-n-ano-nnnnnnnnnnnnn
谢谢.
解决方法
这对你有用吗? (我已经改编了一个我们内部使用的类似脚本.)我相信我们在ColdFusion 8中使用它,因为我们仍然使用它与CF9.
<cffunction name="generateSlug" output="false" returnType="string"> <cfargument name="str" default=""> <cfargument name="spacer" default="-"> <cfset var ret = replace(arguments.str,"'","","all")> <cfset ret = trim(ReReplaceNoCase(ret,"<[^>]*>","ALL"))> <cfset ret = ReplaceList(ret,"À,Á,Â,Ã,Ä,Å,Æ,È,É,Ê,Ë,Ì,Í,Î,Ï,Ð,Ñ,Ò,Ó,Ô,Õ,Ö,Ø,Ù,Ú,Û,Ü,Ý,à,á,â,ã,ä,å,æ,è,é,ê,ë,ì,í,î,ï,ñ,ò,ó,ô,õ,ö,ø,ù,ú,û,ü,ý, ,&","A,A,AE,E,I,D,N,O,U,Y,a,ae,e,i,n,o,u,y,")> <cfset ret = trim(rereplace(ret,"[[:punct:]]"," ","all"))> <cfset ret = rereplace(ret,"[[:space:]]+","!","all")> <cfset ret = ReReplace(ret,"[^a-zA-Z0-9!]","ALL")> <cfset ret = trim(rereplace(ret,"!+",arguments.Spacer,"all"))> <cfreturn ret> </cffunction> <cfset stringToBeSlugged = "This is a string abcde àáâãäå èéêë ìíîï òóôö ùúûü ñ año ñññññññññññññ" /> <cfoutput>"#stringToBeSlugged# = #generateSlug(stringToBeSlugged)#</cfoutput>
支持更多的国际性
如果你想扩大对国际字符的支持,你可以使用ICU4J(java)和Paul Hastings的Transliterator.CFC来音译所有字符,然后用破折号替换任何剩余的空格,破折号,斜线等.
https://gist.github.com/JamoCA/ec4617b066fc4bb601f620bc93bacb57
http://site.icu-project.org/download
安装两者后,您可以通过识别语言ID(要转换为)来转换非拉丁字符并传递要转换的字符串:
<cfset Transliterator = CreateObject("component","transliterator")> <cfoutput> <cfloop array="#TestStrings#" index="TestString"> <h3>TestString = "#TestString#"</h3> <blockquote> <div>CFC-1 = #Transliterator.transliterate('Latin-ASCII',TestString)#</div> <div>CFC-2 = #Transliterator.transliterate('any-NFD; [:nonspacing mark:] any-remove; any-NFC',TestString)#</div> </blockquote> <hr> </cfloop> </cfoutput> <h2>Available Language IDs</h2> <cfdump var="#Transliterator.getAvailableIDs()#" label="Language IDs">