how to replace new line to empty space on coldfusion - coldfusion

<cfoutput>
<cfset str ="hai how are you? i am fine
what about you?" >
<cfdump var="#str#" /><br>
<cfset str = #replace(str,"#chr(13)&chr(10)#"," ","all")#>
<cfdump var="#str#" /><br>
<cfset str = #reReplace(str, "#chr(13)&chr(10)#"," ")#>
<cfdump var="#str#" />
</cfoutput>
Above code produce the this kind of output
hai how are you? i am fine what about you?
hai how are you? i am fine what about you?
hai how are you? i am fine what about you?
my task is replace the new line character but i can't. if any way to do that please let me know?

I can understand your issue but we cannot produce a newline character by cfset. And #chr(13) & chr(10)# is not valid. it should be either chr(13) or chr(10). You can do two replaces one by one. And ReReplace function is used to replace string based on a RegEx. You can use replaceNoCase here. Here is my sample code.
<cfif isDefined("form.submit")>
<cfset str =form.TestField>
<cfoutput>
#findNoCase(Chr(13), str)# <!--- It is available --->
<cfset str = replaceNoCase(str, chr(13), ' ','All')>
<cfset str = replaceNoCase(str, chr(10), ' ','All')>
<cfdump var="#str#"><!--- string after replacement --->
</cfoutput>
</cfif>
<form name="test" method="post">
<textarea name="TestField"></textarea>
<input type="submit"name="submit" value="submit">
</form>

Related

Coldfusion check if input value blank then set in another input

So I've read a lot of answers but none work for me. I need to check and see if the input value is empty (blank), if it is, then put the value of another input to empty as well.
Here is the code:
<input name="Main" type="text" value="" autocomplete="off" />
<input name="Second" type="hidden" value="" />
So when the value of Main is empty, I want to set the value of Second the empty as well.
Here is what I tried:
<cfif len(trim(Main)) EQ 0>
<cfset Second= "">
</cfif>
With scope form
<cfif len(trim(form.Main)) EQ 0>
<cfset Second = form.Second>
</cfif>

How can I keep <sup> & <sub> html tags in my coldfusion string but get rid of all other html tags?

How can I keep <sup>whatever</sup> & <sub>whatever</sub> html tags in my coldfusion string but get rid of all other html tags?
Although there are many ways to run regex magic in CF, I still prefer some Java here to walk through content and capture stuff.
<!--- string with tags to strip --->
<cfsavecontent variable="stringToStrip">
<p class="something">
Hello <sup>World</sup>
</p>
<div>
<div style="border: 1px solid;">foo</div>
<sub class="example">bar</sub>
</div>
</cfsavecontent>
<!--- regex to capture all tag occurences --->
<cfset stripRegEx = "<[^>]+>">
<cfset result = createObject("java", "java.lang.StringBuilder").init()>
<cfset matcher = createObject("java", "java.util.regex.Pattern").compile(stripRegEx).matcher(stringToStrip)>
<cfset last = 0>
<cfloop condition="matcher.find()">
<!--- append content before next capture --->
<cfset result.append(
stringToStrip.substring(
last,
matcher.start()
)
)>
<!--- full tag capture --->
<cfset capture = matcher.group(
javaCast("int", 0)
)>
<!--- keep only sub/sup tags --->
<cfif reFindNoCase("</?su[bp]", capture)>
<cfset result.append(capture)>
</cfif>
<!--- continue at last cursor --->
<cfset last = matcher.end()>
</cfloop>
<!--- append remaining content --->
<cfset result.append(
stringToStrip.substring(last)
)>
<!--- final result --->
<cfset result = result.toString()>
<cfoutput>#result#</cfoutput>
Output is:
Hello <sup>World</sup>
foo
<sub class="example">bar</sub>
I think you could also use a negative lookahead in a regex replace like so:
stripped_string = reReplaceNoCase(source_string, '<(?!/?su[bp]\b)[^>]+>', '', 'all' );

scraping data hurdles with coldfusion

I am working on a scraping and i need to extract the informaion of the websites, basically it is giving link to inside pages
Here is my Small try with scraping, i am able to get the names of the states but i need to go deeper now as i admit, i am very bad at regular expressions
Please guide. here is my code
<cfscript>
function stripHTML(str) {
str = reReplaceNoCase(str, "<*style.*?>(.*?)</style>","","all");
str = reReplaceNoCase(str, "<*script.*?>(.*?)</script>","","all");
str = reReplaceNoCase(str, "<.*?>","","all");
str = reReplaceNoCase(str, "^.*?>","");
str = reReplaceNoCase(str, "<.*$","");
return trim(str);
}
</cfscript>
<cfset start = false>
<cfhttp url="http://www.mapsofindia.com/pincode/" method="get"></cfhttp>
<cfset status = cfhttp.Statuscode>
<cfif status IS '200 OK'>
<cfset start = true>
</cfif>
<cfif start>
<cfset string = cfhttp.filecontent />
<cfset StartText = '<table cellpadding=4 cellspacing=0 align="center" border="0" class=extrtable width="90%">' />
<cfset Start = FindNoCase(StartText, string, 1) />
<cfset EndText='<td style="width:50%"> </td></tr></table><br /><br />' />
<cfset Length=Len(StartText) />
<cfset End = FindNoCase(EndText, string, Start) />
<cfset parse = Mid(string, Start+Length, End-Start-Length) />
<cfset parse = Insert('+',parse,FindNoCase("</a>", parse))>
<cfset parse = trim(parse) />
<cfset dbData = stripHTML(ListQualify(parse,'+'))>
<cfloop list="#createLst#" index="k" delimiters="+">
<cfquery name="insert_data" datasource="#request.dsn#">
INSERT INTO cw_states(state,countrycode,country)
VALUES('#k#','IN','India')
</cfquery>
</cfloop>
<table align="center">
<cfoutput>#parse#<br></cfoutput>
</tr>
</table>
</cfif>
This is listing the cfoutput on page correctly, but i am unable to insert into the database.
Question #2: The link is taking to another page which lists the cities of the state, i want to append my page like index2.cfm?url=#link# which will extract the cities and insert the cities in my cities table and again on the link it should be like index3.cfm?url=#link# which will open another page of zip and insert those zip names in the database
Please guide

Write Coldfusion query results to a text file not working

<cfquery name="writefile" datasource="#dsn#">
SELECT abc,def,pqr,stu,zex
FROM mytable
</cfquery>
<cfoutput>
<table>
<cfloop query="writefile">
<tr>
<cfloop list="#ArrayToList(writefile.getColumnNames())#" index="col">
<cffile action="write" file="d:\test.txt" output="#writefile[col][currentrow]#">
</cfloop>
</tr>
</cfloop>
</table>
</cfoutput>
I am using the above code to write a text file to a location using cffile.
But the text file is not containing all the results of the query. Please guide me.
Using cffile action="write" will reset the file each time.
Use action="append" to add content to a file without first blanking the file.
You should also consider building the string first, then writing to the file in a single action.
For example:
<cfset Content = "" />
<cfloop query="writefile">
<cfloop array=#writefile.getColumnNames()# index="col">
<cfset Content &= ' ' & writefile[col][currentrow] />
</cfloop>
<cfset Content &= chr(10) />
</cfloop>
<cffile action="write" file="d:\test.txt" output="#FileContent#" />
(Note: string concatenation used for simplicity - if performance matters, consider using StringBuilder and/or cfsavecontent instead.)

Coldfusion REReplace (to parse Twitter Feed)

I have a twitter feed in the format:
1. Username: Blah blah http://something.com #hashtag
2. Username: Blah blah http://something.com #hashtag
3. Username: Blah blah http://something.com #hashtag
I'm removed the username, but how do I wrap tags (for styling) around the http:// parts and the #hashtags?
Here is my current coldfusion code:
<cfset feedurl="http://twitter.com/statuses/user_timeline/MyUserID.rss" />
<cffeed
source="#feedurl#"
properties="feedmeta"
query="feeditems" />
<cffeed
source="#feedurl#"
properties="feedmeta"
query="feeditems" />
<ul>
<cfoutput query="feeditems">
<cfsavecontent variable="twitterString">
#content#
</cfsavecontent>
<li>#REReplace(twitterString, "UserName: ", "")#</li>
</cfoutput>
</ul>
This worked for me:
<cfset feedurl="http://twitter.com/statuses/user_timeline/jakefeasel.rss" />
<cffeed
source="#feedurl#"
properties="feedmeta"
query="feeditems" />
<cffeed
source="#feedurl#"
properties="feedmeta"
query="feeditems" />
<ul>
<cfoutput query="feeditems">
<cfsavecontent variable="twitterString">
#REReplace(content, "UserName: ", "")#
</cfsavecontent>
<cfset formattedString = twitterString>
<cfloop array='#[{"regex" = 'http://\S+', "class" = "URL"}, {"regex" = "##\w+", "class" = "hashTag"}]#' index="regexStruct">
<cfset currentPos = 0>
<cfset matches = ReFindNoCase(regexStruct.regex, twitterString, currentPos, true)>
<cfloop condition="matches.len[1] IS NOT 0">
<cfset formattedString = Replace(formattedString, mid(twitterString, matches.pos[1], matches.len[1]), "<span class='#regexStruct.class#'>" & mid(twitterString, matches.pos[1], matches.len[1]) & "</span>")>
<cfset currentPos = matches.pos[1] + matches.len[1]>
<cfset matches = ReFindNoCase(regexStruct.regex, twitterString, currentPos, true)>
</cfloop>
</cfloop>
<li>
#formattedString#
</li>
</cfoutput>
</ul>
You'll obviously have to provide styles for the "URL" and "hashtag" classes.