Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 11-15 of 15 total

Replace line breaks with HTML breaks

Replace(check1.comments, chr(13) & chr(10), "<br>","ALL")

Getting and Parsing 10x10

Here's my source code in ColdFusion for parsing 10x10. I can't imagine it being different in other languages.

----

<!--- build the wordsURL before getting the data --->
<cfset year = DateFormat(Now(),"YYYY")>
<cfset month = DateFormat(Now(),"MM")>
<cfset day = DateFormat(Now(),"DD")>
<cfset hour = TimeFormat(Now(),"HH")>
<cfset wordsURL = "http://tenbyten.org/Data/#year#/#month#/#day#/#hour#/words.txt">

<!--- connect to the url --->
<cfhttp method="get" url="#wordsURL#" timeout="60"></cfhttp>

<cfset wordlist = cfhttp.FileContent>

<!--- write the data into a text file --->
<cffile action="write" file="#ExpandPath('words.txt')#" output="#ToString(wordlist)#">


----

Reading the content is as simple as opening the file and traversing through it like so. I also added in a link to Google news. I've found it helpful to click on news bits in the past.

----

<cfhttp method="get" url="http://path/to/words.txt" timeout="60"></cfhttp>

<cfset wordlist = cfhttp.FileContent>

<ul><li>
<cfloop list="#wordlist#" delimiters="#chr(13)##chr(10)#" index="word">
  <cfoutput>
    <a href="http://news.google.com/news?q=#word#" target="_blank">#word#</a>
  </cfoutput>
</cfloop>
</li></ul>

Removing duplicates from lists

A request for this came up on the CF-Talk list recently. Here's what I came up with.
<!--- The list with want to clean up --->
<cfset lst = "a,list,with,a,few,duplicates">
<!--- Use a struct to build a set of the list elements --->
<cfset set = StructNew()>
<cfloop index="elem" list="#lst#">
    <cfset set[elem] = "">
</cfloop>
<!--- Convert the set back to a list --->
<cfset lst = StructKeyList(set)>

This sort of this should be familiar to all you perlmongers out there.

Directory listing

<cfdirectory action="LIST" directory="d:\www\docs\" name="dirlist">
<cfloop query="dirlist">
	<cfset outSize = dirlist.size / 1000>
	<a href="docs/#dirlist.name#" target="_blank">#dirlist.name#</a> 
	(#NumberFormat(outSize)# kB, 
	#lsdateformat(dirlist.dateLastModified, 'dddd d mmmm yyyy')#)
</cfloop>

Remove HTML from string

REReplaceNoCase(string,"<[^>]*>","","ALL")
« Newer Snippets
Older Snippets »
Showing 11-15 of 15 total