CFLib.org – Common Function Library Project

splitUrl(inUrl)

Last updated January 12, 2004

author

Samuel Neff

Version: 1 | Requires: CF5 | Library: StrLib

Description:
Use this UDF to safely split out or manipulate one portion of a URL.

Return Values:
Returns a struct.

Example:

<cfset ref = splitUrl("http://www.mysite.com/index.cfm?var=val##anchor")>
<cfdump var="#ref#">

Parameters:

Name Description Required
inUrl URL to parse. Yes

Full UDF Source:

/**
 * Splits a URL into it's URL, QueryString, and Anchor parts.
 * 
 * @param inUrl 	 URL to parse. (Required)
 * @return Returns a struct. 
 * @author Samuel Neff (sam@serndesign.com) 
 * @version 1, January 12, 2004 
 */
function splitUrl(inUrl) {
	var s = inUrl;
	var refUrl = "";
	var refQS = "";
	var refAnchor = "";
	var st = structNew();
	var i = find("?", s);
	
	if (i) {
		refUrl = left(s, i-1);
		refQS = mid(s, i+1, 99999);
		i = find("##", refQS);
		if (i) {
			refAnchor = mid(refQS, i+1, 99999);
			refQS = left(refQS, i-1);
		} else {
		refAnchor = "";
		}
	} else {	
		i = find("##", s);
		if (i) {
			refUrl = left(s, i-1);
			refAnchor = mid(s, i+1, 99999);
		} else {
			refUrl = s;
		}
	}
	
	st.url = refUrl;
	st.queryString = refQS;
	st.anchor = refAnchor;
	
	return st;
}
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Kevin Cotton added
date2ExcelDate
May 5, 2016

Raymond Camden added
CapFirst
April 25, 2016

Chris Wigginton added
loremIpsum
January 18, 2016

Gary Stanton added
calculateArrival...
November 19, 2015

Sebastiaan Naafs - van Dijk added
getDaysInQuarter
November 13, 2015

Created by Raymond Camden / Design by Justin Johnson