I’ve worked up a pretty basic UDF for formatting a URL. If you have an application that allows users to input a url you may notice that they have about a million different ways of entering it. What we as developers need however is a full address. That means we need the ‘http://’ included. Most user’s however, would never include this in their entered URL.
This UDF will detect how much of a URL the user entered, and set the formatting as is needed. It accepts three arguments. First is the URL to be formatted. Second is a boolean specifying whether or not to append ‘http://’ if necessary. Third is a boolean specifying whether or not to append ‘www.’ if needed. If the second argument is set to true the third argument will be set to true by default. This is based on the assumption that if you have ‘http://’ then you would also want ‘www.’. Remember that this is a fairly basic method. It will detect secure protocols, but will not detect subdomains and such. It also pays no attention to the ‘.com’, ‘.org’, etc. So feel free to use it if it fits your needs.
<!--- ******************************** FORMAT URL ******************************** ---> <cffunction name="formatUrl" displayname="Format URL" description="Formats a URL for appropriate web use." access="public" output="false" returntype="struct"> <!--- PASS IN ARGUMENTS ---> <cfargument name="url" displayName="URL" type="string" hint="URL to be formated" required="true" /> <cfargument name="includehttp" displayName="Include HTTP" type="boolean" hint="Indicate whether or not to include HTTP" default="true" required="false" /> <cfargument name="includewww" displayName="Include www" type="boolean" hint="Whether or not to include the www. If includehttp is true this must also be true." default="true" required="false" /> <!--- SET SOME LOCAL VARS ---> <cfset var charcontainer = ""> <cfset var protocol = "http://"> <cfset var protocollength = 7> <!--- CREATE STRUCT ---> <cfset var data = structnew()> <cfset data.success = true> <cfset data.message = ""> <cfset data.url = ARGUMENTS.url> <cftry> <!--- CHECK IF SSL IS USED ---> <cfif left(trim(data.url), 8 ) eq "https://"> <cfset protocol = "https://"> <cfset protocollength = 8> </cfif> <!--- IF ARGUMENTS.INCLUDEHTTP IS TRUE MAKE SURE ARGUMENTS.INCLUDEWWW IS TRUE ALSO ---> <cfswitch expression="#ARGUMENTS.includehttp#"> <!--- ADD HTTP:// AND WWW. IF NECESSARY ---> <cfcase value="true"> <!--- SET ARGUMENTS.INCLUDEWWW TO TRUE ---> <cfset ARGUMENTS.includewww = true> <!--- CHECK IF HTTP INCLUDED IN URL ---> <cfif left(trim(data.url), protocollength) neq protocol> <!--- CHECK IF THE WWW. IS INCLUDED ---> <cfif left(trim(data.url), 4) eq "www."> <cfset data.url = protocol & data.url> <cfelse> <cfset data.url = protocol & "www." & data.url> </cfif> <cfelse> <!--- REMOVE THE HTTP, IT WILL BE ADDED BACK ---> <cfset data.url = replace(data.url, protocol, "", "one")> <!--- REMOVE THE WWW. ---> <cfset data.url = replace(data.url, "www.", "", "one")> <!--- CHECK IF WWW INCLUDED IN URL ---> <cfset charcontainer = left(trim(data.url), 4 + protocollength)> <cfif right(trim(charcontainer), 4) neq "www."> <cfset data.url = protocol & "www." & data.url> </cfif> </cfif> </cfcase> <!--- REMOVE HTTP:// ---> <cfdefaultcase> <!--- CHECK IF HTTP INCLUDED IN URL ---> <cfif left(trim(data.url), protocollength) eq protocol> <cfset data.url = replace(data.url, protocol, "", "one")> <!--- CHECK IF WWW. SHOULD BE INCLUDED ---> <cfswitch expression="#ARGUMENTS.includewww#"> <!--- MAKE SURE WWW. IS INCLUDED ---> <cfcase value="true"> <cfif left(trim(data.url), 4) neq "www."> <cfset data.url = "www." & data.url> </cfif> </cfcase> <!--- REMOVE WWW. IF NECESSARY ---> <cfdefaultcase> <cfif left(trim(data.url), 4) eq "www."> <cfset data.url = replace(data.url, "www.", "", "one")> </cfif> </cfdefaultcase> </cfswitch> <cfelse> <!--- CHECK IF WWW. SHOULD BE INCLUDED ---> <cfswitch expression="#ARGUMENTS.includewww#"> <!--- MAKE SURE WWW. IS INCLUDED ---> <cfcase value="true"> <cfif left(trim(data.url), 4) neq "www."> <cfset data.url = "www." & data.url> </cfif> </cfcase> <!--- REMOVE WWW. IF NECESSARY ---> <cfdefaultcase> <cfif left(trim(data.url), 4) eq "www."> <cfset data.url = replace(data.url, "www.", "", "one")> </cfif> </cfdefaultcase> </cfswitch> </cfif> </cfdefaultcase> </cfswitch> <cfcatch type="any"> <cfset data.success = false> <cfset data.message = "There was a problem formatting the url."> </cfcatch> </cftry> <!--- RETURN STRUCT ---> <cfreturn data> </cffunction>