Search code examples
coldfusioncoldfusion-9

Coldfusion-9 Trim Values


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.


Solution

  • 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>