What is the correct syntax for replacing two characters at once in ColdFusion?
I'm trying to replace two special characters using the replace function in ColdFu开发者_StackOverflow中文版sion:
<cfset MyQuery = "/Attribute One\/Attribute Two\/Attribute Three\">
<cfset MyString = Replace(MyQuery, "/", "<li>", "ALL")>
<ul>
<cfoutput>#MyString#</cfoutput>
</ul>
This works well, but I need to correctly close my li tags. I tried adding the following which did not work:
<cfset MyString = Replace(MyQuery, "/", "<li>", "ALL") AND Replace(MyQuery, "\", "</li>", "ALL")>
First question: Is this the right way to what I'm attempting? Or should I just store my html inside the database along with the attributes?
Second question: If my approach is correct, could someone please provide an example of the correct syntax?
Many thanks!
Nested replace? Just replace "/" first to avoid affecting "":
<cfset MyQuery = "/Attribute One\/Attribute Two\/Attribute Three\" />
<cfset MyString = Replace(MyQuery, "/", "<li>", "ALL") />
<cfset MyString = Replace(MyString, "\", "</li>", "ALL") />
<cfoutput>#MyString#</cfoutput>
<cfset MyString = Replace(Replace(MyQuery, "/", "<li>", "ALL"), "\", "</li>", "ALL") />
<cfoutput>#MyString#</cfoutput>
List manipulation can be another the way to do this:
<cfset MyQuery = "/Attribute One\/Attribute Two\/Attribute Three\">
<cfset MyString = "" />
<cfloop list="#MyQuery#" index="li" delimiters="\/">
<cfset MyString &= "<li>#li#</li>" />
</cfloop>
<cfoutput>#MyString#</cfoutput>
精彩评论