I would like to translate some values in the comments section in my posts to HTML. So, in fact, I want to translate values in (CFOUTPUT) #comments# such as [b], [/b], [i] and [/i] to their HTML counterparts. I have no idea how I should do this though, and that is why I'm asking help on Stackoverflow.
My best guess is to achieve this by using the RePlace function, but I have no idea how.
(Posted from comments)
<cfoutput>#REReplace(gettopic.text,"[b]","<b>","ALL")#</cfoutput>
<cfoutput>#REReplace(gettopic.text,"[/b]","</b>","ALL")#</cfoutput>
<cfoutput>#REReplace(gettopic.text,"[i]","<i>","ALL")#</cfoutput>
<cfoutput>#REReplace(gettopic.text,"[/i]","</i>","ALL")#</cfoutput>
Can't do anything else as I don't understand how to combine
It's pretty obvious you haven't read much of the docs as they would have told you what you're doing wrong, but I'll go ahead and spare everyone elses time.
You need to set a variable and then clean that variable rather than just outputting your original string. You would be well served checking out this tutorial which explains how to set a variable or really any general programming tutorial as this is extremely basic stuff . Something you would learn the first day of class or the first section of a tutorial
<cfset cleanString = replace(gettopic.text,"[b]","<b>","ALL")>
<cfset cleanString = replace(cleanString,"[/b]","</b>","ALL")>
<cfset cleanString = replace(cleanString,"[i]","<i>","ALL")>
<cfset cleanString = replace(cleanString,"[/i]","</i>","ALL")>
<cfoutput>
#cleanString#
</cfoutput>
You can also use the replaceList function
<cfset cleanString = replaceList(gettopic.text,'[b],[/b],[i],[/i]','<b>,</b>,<i>,</i>')>
<cfoutput>
#cleanString#
</cfoutput>
Related
I'm stuck and need a fresh set of eyes on this, please.
I'm working with someone else's spaghetti code who is no longer around and having a heck of a time figuring out what they were evaluating.
<cfset surveyCount = 0>
<cfloop query="surveys">
<cfif evaluate("defaultReport" & ID)>
<cfset surveyCount = surveyCount + 1>
</cfif>
</cfloop>
In the query dump, I see 9 records which is what I am expecting but because because the evaluate is failing, the surveyCount isn't being incremented. I do not see any columns for defaultReport. In my 15 years of working with CF, I've always avoided evaluate() and now when I need to analyze it, I'm at a complete loss. Can someone offer any guidance?
Added CFDump image (some columns names have been removed for privacy and security):
UPDATE I:
This file has numerous cfinclude statements and very little code formatting. As a result, I overlooked some cfinclude statements. I found the following. I'm still looking but wanted to document this as I dig.
<cfloop query="surveys">
<cfscript>
variables["defaultReport" & ID] = evaluate(thisAssociation & "Price");
</cfscript>
</cfloop>
UPDATE II:
Dumping the variable scope, I did confirm the variable I am looking for (finding the query I posted in UPDATE I helped, too). :)
What they wanted to do is to increase surveyCount but only if this thing: evaluate("defaultReport" & ID) evaluates to true.
From your query dump picture it looks like the IDs are numbers like 144, 145, etc...
In this context, you can think at evaluate("defaultReport" & ID) as something like defaultReport144, defaultReport145, etc... (these are variables set somewhere in the code).
So the code:
<cfif evaluate("defaultReport" & ID)>
<cfset surveyCount = surveyCount + 1>
</cfif>
becomes (for an ID of 144, the first one on your query loop)
<cfif defaultReport144>
<cfset surveyCount = surveyCount + 1>
</cfif>
and so on... for the other IDs
So, search your code for where variables like defaultReport144, defaultReport145, etc... are set to either true or false (0 or 1).
Something like:
<cfset defaultReport144 = true />
or maybe they use some expression that evaluates to true or false, like:
<cfset defaultReport144 = [some expression] />
If you can't find, then maybe the code was changed or removed in the place where these defaultReport... variables were set.
ColdFusion evaluate() documentation:
https://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-7f4e.html
You need to look for a variable outside of your query. This variable has a name of default#ID# . It may be called.
variables.default#ID#
form.default#ID#
url.default#ID#
request.default#ID#
attributes.default#ID#
etc.
Basically ColdFusion is going to go through every scope until it finds something. (No this is not a good approach)
If you have to clean this up, I would recommend not using such an ambiguous approach. In short, there is no real way to know what it is evaluating.
Forgive the newbie question here, but I am trying to allow for custom variables in a string that I would pull from a query, and replace those with results of another query. So to simplify it, I would need something like this...
<cfset mystring = "This is my %firstname% and this is my %lastname%.">
Then suppose I had query results of MyQuery,first and MyQuery.last, I would like the most efficient way to replace the %firstname% and %lastname% in mystring with the query results.
Any help/example would be so appreciated!
Thanks.
Are you looking for replaceNoCase()?
<cfset myNewString = replaceNoCase(myString, '%firstname%', myQuery.first, 'all')>
<cfset myNewString = replaceNoCase(myNewString, '%lastname%', myQuery.last, 'all')>
if you had a query like this:
<cfquery name="myquery" datasource="mydatasource">
select firstname, lastname from users
</cfquery>
then you would out put the values from that by doing the following:
<cfoutput query="myquery">
<cfset variables.mystring = "This is my #firstname# and this is my #lastname#.">
</cfoutput>
If you are using Lucee then you can take advantage of replace(string, struct) (showing cfscript syntax):
template = "This is my %firstname% and this is my %lastname%.";
replaceMap = {
"%firstname%" : "John"
,"%lastname%" : "Doe"
}
populated = replace(template, replaceMap); // use replaceNoCase() for case-insensitivity
All of the replacements take place in the Java code which is much more efficient than doing it in a CFML loop.
Also, replace() is much more efficient than replaceNoCase(), so if the CaSe is known in advance you should use replace() where possible, or consider normalizing the case, e.g. with lcase(), prior to calling replace().
I'm trying to teach myself ColdFusion.
I have a string coming in from the database in this format:
domain.com
<br/>
www.facebook.com/facebookpage
<br/>
http://instagram.com/instagrampage
It is all coming from #getRacer.txtDescription#. The format of this text will always be the same.
I need to split it into 3 variables. I tried this (derived from the example on the adobe website)
<h3>ListToArray Example</h3>
<cfset myList = ValueList(getRacer.txtDescription)>
<p>My list is a list with <cfoutput>#ListLen(myList)#</cfoutput> elements.
<cfset myArrayList = ListToArray(myList,'<br/>')>
<p>My array list is an array with
<cfoutput>#ArrayLen(myArrayList)#</cfoutput> elements.
I somehow ended up with 11 items in the array.
Thank you
This should work.
<cfset TestSTring = "domain.com<br/>www.facebook.com/facebookpage<br/>http://instagram.com/instagrampage">
<cfset a = TestString.Split("<br/>")>
The reason ListtoArray is displaying 11 items is because ColdFusion treats each character in the delimiter string (<br/>) as a separate delimiter
Based on #Leigh's comment updating my answer to ensure people should learn the Coldfusion APIs rather than fiddling with Java functions, <cfset a = ListToArray(TestString, "<br/>", false, true)> will also work. Thanks Leigh.
Note: The false at the end is for the includeEmptyFields flag and the true is for the multiCharacterDelimiter flag. See the docs.
<cfset myList = ReplaceNoCase(getRacer.txtDescription,'<br/>','|','ALL')>
<cfset myArrayList = ListToArray(myList,'|')>
I chose a pipe character because it is unlikely to already exist in your string. If you wanted to account for the possibility that your BR tag may or may not use XML syntax then you could you regex:
<cfset myList = ReReplaceNoCase(str,'<br/?>','|','ALL')>
<cfset myArrayList = ListToArray(myList,'|')>
We currently have some application variables being defined on our apps and also have a special set of tags that are processed within our templates. However I'm finding it to be a bit difficult to split the tags, so I can convert them to application variables when parsed by the template.
<cfset mystring = "[pss]'fetchMessages','VWO-Tracking-Code'[end_pss]">
<cfset the_match = REMatch("\[pss\]\s*(((?!\[pss\]|\[end_pss\]).)+)\s*\[end_pss\]",mystring) />
<cfdump var="#the_match#" />
Our goal is to split the strings between the "[pss] and [end_pss]"
The regex above simply takes the string and applies it to a CF array, which is all good. However I strictly want the code between it as I'll be able to convert it to
<cfset application.snippets['VWO-Tracking-Code']>
Right now it returns everything as one string and I also require the first portion to determine what type of functionality is required.
Any ideas on how to do this would be greatly appreciated.
We are currently using CF 8 and the structure of the code is exactly the same all the time. The only thing is it can be contained within other strings of code as well...
If you're only doing this for one set of tags you can use the replaceList function rather than regEx
<cfset mystring = "[pss]'fetchMessages','VWO-Tracking-Code'[end_pss]">
<cfset mystring = replaceList(mystring,"[pss],[end_pss],'",'')>
<cfset firstitem = listfirst(mystring)>
<cfset seconditem = listlast(mystring)>
<cfdump var="#firstitem#">
<cfdump var="#seconditem#">
This outputs fetchMessages and VWO-Tracking-Code. If you want/need the single quotes on your strings then you can remove the ,' from the 2nd parameter of the replaceList function.
I have HTML code as String variable in Coldfusion.
For example:
<cfset str = "<span class='xyz'> sample text within span </span>" >
Now I want to repalce the word span from text "sample text within span" but not the tag name.
Can someone help me?
Thanks in advance.
I'm going to dumbly answer your question:
<cfset str = "<span class='xyz'> sample text within span </span>" >
<cfdump var="#str#" />
<!--- Convert to list based on start and end tag brackets --->
<cfset arr = listToArray(str, ">,<") />
<!--- Replace the ACTUAL text --->
<cfset newStr = replace(str, arr[2], "my new text") />
<cfdump var="#newStr#" />
Disclaimer: if I caught myself writing this I would probably think myself on the wrong track.
This road leads to one trying to use regular expressions to parse HTML, regarded as a bad thing as mentioned in this article.
Perhaps you can explain your problem a little more and we can help.
I habe changed my code like this and it seems to work, but how efficent it is I don't know
Can someone please check it?
<cfset htmlcontents = ReReplaceNoCase(htmlcontents, "(>[^<]*?)(#Lcase(text2replace)#)", "\1<span class=suchehighlight>\2</span>","ALL")>
Thanks