Standardizing Your URL Data in ColdFusion
We are currently developing coding guidelines and I wanted make URLs were valid and consistently formatted before they were inserted into our SQL tables. Here is a script to standardize the URLs you receive through form data.
This is my first version of a URL cleaning script. It takes a variable that is assumed to be a URL. Then, it takes that string and checks to see if it does not contain http://. The script attempts to load that page and if the header code of the page is 200 OK, which means it was a good load, the variable is trimmed and lower cased. Also, the script checks for three different versions of the URL.
Start: #dirtyURL#
Temp1: #tempURL#
Temp2: #tempURL#
Temp3: #tempURL#
Finish: #cleanURL#
I added the INVALID: prepend to the bad URLs to represent that the URL was not successfully connected. One could run another script on the table to resolve those erroneous URLs.
Like I said, this was my first attempt to standardize the URLs that we receive through form data. This code hasn’t been tested enough to deem it bulletproof. I don’t claim to be a code ninja, so if you know of a better method or would like to tweak this version, please let me know.



No comments yet.