Coldfusion正则表达式产生slu ..

时间:2016-04-26 05:50:20

标签: regex function coldfusion slug

我有这个函数在Coldfusion中生成slu ::

<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", "all") />
    <cfset str = reReplace(str, "[ìíîï]", "i", "all") />
    <cfset str = reReplace(str, "[òóôö]", "o", "all") />
    <cfset str = reReplace(str, "[ùúûü]", "u", "all") />
    <cfset str = reReplace(str, "[ñ]", "n", "all") />
    <cfset str = reReplace(str, "[^a-z0-9-]", "#spacer#", "all") />
    <cfset ret = reReplace(str, "#spacer#+", "#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 ::

这是一个字符串abcde-a-a-a-a-a-e-e-e-i-i-i-o-o-o-u-u-u-u-a-no-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 

感谢。

1 个答案:

答案 0 :(得分:3)

这对你有用吗? (我已经改编了一个我们在内部使用的类似脚本。)我相信我们在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, "À,Á,Â,Ã,Ä,Å,Æ,È,É,Ê,Ë,Ì,Í,Î,Ï,Ð,Ñ,Ò,Ó,Ô,Õ,Ö,Ø,Ù,Ú,Û,Ü,Ý,à,á,â,ã,ä,å,æ,è,é,ê,ë,ì,í,î,ï,ñ,ò,ó,ô,õ,ö,ø,ù,ú,û,ü,ý,&nbsp;,&amp;", "A,A,A,A,A,A,AE,E,E,E,E,I,I,I,I,D,N,O,O,O,O,O,0,U,U,U,U,Y,a,a,a,a,a,a,ae,e,e,e,e,i,i,i,i,n,o,o,o,o,o,0,u,u,u,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来音译所有字符,然后用破折号替换任何剩余的空格,破折号,斜线等。 / p>

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">