Reputation: 3200
I am trying to write a code that takes a URL that has 3 parts (www).(domainname).(com) and trim the first part out completely.
So far I have this code that checks if on the left side I don't have a 'www' or 'dev'
go in and set siteDomainName = removecharsCGI.SERVER_NAME,1,2);
if (numHostParts eq 3 and listfindnocase('www,dev',left(CGI.SERVER_NAME,3)) eq 0) {
siteDomainName = removecharsCGI.SERVER_NAME,1,2);
The problem with the code above is that is deleting only 2 characters where I need it to delete ALL characters until numHostParts eq 2
or at least until the first "."
Another example would be:
akjnakdn.example.com I need the code to delete the first part of the URL with the dot included (akjnakdn.)
This code will help some of the queries that i have on the site to stop crushing because they are related with the #URL# and when the #URL# is fake I am getting cform query returned zero records
error that is causing my contact forms to stop working.
Upvotes: 3
Views: 640
Reputation: 2924
You could do something like this:
<cfscript>
local.nameArr = ListToArray(CGI.SERVER_NAME, '.');
if (ArrayLen(local.nameArr) gt 2) {
ArrayDeleteAt(local.nameArr, 1);
}
siteDomainName = ArrayToList(local.nameArr, '.');
</cfscript>
I've split the server name into array elements with a period as the delimiter. If the number of elements is greater than two, remove the first element. Then convert it back to a list with the period as a delimiter.
UPDATE
As suggested by Robb, this could be more concise and perform better by skipping the array conversion process:
<cfscript>
siteDomainName = CGI.SERVER_NAME;
if (ListLen(siteDomainName, '.') gt 2) {
siteDomainName = ListDeleteAt(siteDomainName, 1, '.');
}
</cfscript>
Upvotes: 4
Reputation: 12495
I would use a regular expression, since you only want to "trim" certain subdomains (www
,dev
).
<cfset the_domain = REReplaceNoCase(cgi.SERVER_NAME, "(www|dev)\.", "") />
Just use a |
-delimited list of subdomains you want to trim within the parentheses.
Upvotes: 0
Reputation: 3884
You can just use listRest
. It returns all the elements in a list, except the first one. Documentation is here http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-6d87.html
Example:
<cfscript>
name = cgi.server_name;
if (listlen(name,".") gte 3) {
name = listRest(name,".");
}
</cfscript>
Upvotes: 6