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;
}

Search CFLib.org


Latest Additions

Raymond Camden added
QueryDeleteRows
November 04, 2017

Leigh added
nullPad
May 11, 2016

Raymond Camden added
stripHTML
May 10, 2016

Kevin Cotton added
date2ExcelDate
May 05, 2016

Raymond Camden added
CapFirst
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson