CFLib.org – Common Function Library Project

GetUsernameFromURL(this_url)

Last updated August 23, 2002

author

Shawn Seley

Version: 2 | Requires: CF5 | Library: StrLib

Description:
Returns the username (ending with either ":" or "@") for the passed URL. If no user is found, then returns an empty string. Works with any protocol that follows a "username:password@" syntax including ftp, telnet, and imap and others.

Return Values:
Returns a string.

Example:

<cfoutput>
#GetUsernameFromURL("telnet://guest:pass@somehost.com:21/")#<br>
#GetUsernameFromURL("guest:pass@somehost.com")#<br>
#GetUsernameFromURL("http://host.com/")#<br>
#GetUsernameFromURL("ftp://me:@host.com/")#<br>
#GetUsernameFromURL("ftp://me@host.com/")#<br>
#GetUsernameFromURL("ftp://:@host.com/")#<br>
</cfoutput>

Parameters:

Name Description Required
this_url URL to parse. Yes

Full UDF Source:

/**
 * Returns the username from a specified URL.
 * Modified to handle differences in regex in cf5/mx. Thanks to Tom Lane for pointing out the issue.
 * 
 * @param this_url 	 URL to parse. (Required)
 * @return Returns a string. 
 * @author Shawn Seley (shawnse@aol.com) 
 * @version 2, August 23, 2002 
 */
function GetUsernameFromURL(this_url) {
	var first_char       = "";
	var re_found_struct  = "";
	var num_expressions  = 0;
	
	this_url = trim(this_url);
	
	first_char = Left(this_url, 1);
	if (Find(first_char, "./")) {
		return "";   // relative URL = no username (ex: "../dir1/filename.html" or "/dir1/filename.html")
	} else if(Find("://", this_url)){
		// absolute URL    (ex: "ftp://user:pass@ftp.host.com")
		re_found_struct = REFind("(([^:@]*:)[^:@]*@)|([^:@]*@)", this_url, Find("://", this_url)+3, "True");
	} else {
		// abbreviated URL (ex: "user:pass@ftp.host.com")
		re_found_struct = REFind("(([^:@]*:)[^:@]*@)|([^:@]*@)", this_url, 1, "True");
	}
	
	if (re_found_struct.pos[1] GT 0) {
		num_expressions = ArrayLen(re_found_struct.pos);
		if(re_found_struct.pos[num_expressions] is 0) num_expressions = num_expressions - 1;
		return Mid(this_url, re_found_struct.pos[num_expressions], re_found_struct.len[num_expressions]-1);
	} else {
		return "";
	}
}
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