Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 119
  • Last Modified:

Coldfusion RegEx

Hi,
I'm using CF 11.
I need to pull the URL that the page is on and see if there is a subdomain present. If so, I need to store that subdomain name, in a variable. I could easily do this in Perl or Python, but I have no idea how to do it in CF.

To be clear:
<cfset currentURL = "#CGI.SERVER_NAME#">
<cfoutput>#currentURL#</cfoutput>
Let's say 'currentURL' has   paul.beatles.com in it.
And I want to store 'paul' in a var to later check and see if 'paul' matches a particular string I'm looking for.

How would I do that in CF?  Mostly I need to know how to pull 'paul' out of paul.beatles.com and store 'paul' in a var.

Thanks!
Nacht
0
nachtmsk
Asked:
nachtmsk
  • 4
  • 3
1 Solution
 
Terry WoodsIT GuruCommented:
Perhaps something like this? What kind of values do you get in the currentURL variable?
<cfset subdomain = REReplace( currentURL, "\.[^.]+\.com$",  "" ) />

Open in new window

1
 
nachtmskAuthor Commented:
Hi Terry,
Thanks for the reply.

The currentURL var will either have something like  www.mydomain.com or  someothername.mydomain.com
I will need to ignore  the 'www' subdomain, but save the 'someothername' subdomain in another var.
0
 
dgrafxCommented:
<cfscript>
subdomain="";
if(listlen(cgi.server_name,".") > 2 && !isNumeric(replace(cgi.server_name,".","","all")) && listfirst(cgi.server_name,".") != "www") {
      subdomain=listfirst(cgi.server_name,".");
}
writeoutput(subdomain);
</cfscript>
1
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
nachtmskAuthor Commented:
Worked great. Thanks.
0
 
dgrafxCommented:
The answer you accepted won't do what you asked for.
Did you accept the wrong answer?
Could you explain please ...
0
 
nachtmskAuthor Commented:
dgrafx --
it did do what I asked. It pulled the subdomain out and stored it in a variable.
His answer was also simple and easy to follow.
I appreciated your input, but your answer was overly complicated for my needs.
0
 
dgrafxCommented:
Thanks for responding = however the solution you accepted will not do as you asked even though as you say it is simpler.
Remember asking for an exception for the "www" subdomain?

The solution accepted will return the following:
www.domain.com = www
domain.com = domain.com
xyz.domain.net = xyz.domain.net

and so on and so forth
the only situation it works in is subdomain.domain.com
it doesn't function correctly at www.domain.com or if a user navigates to domain.com or if you are at a .net or .io or any other tld besides .com

Thanks for listening & good luck ...
0
 
nachtmskAuthor Commented:
Hi Again Dgraf --

I appreciate your being so thorough.  
I can ignore www if returned. Also, it's fine that it only works for .com. This is only needed for one domain name, which has the potential for many subdomains and it works perfectly for the isolated case in which I need it. I could have been more precise in my original post through, your correct. I'm sorry I wasn't.

Thanks again for your thoroughness !
I'm sure I will post more questions in the future on CF, as it's not something I have a lot of experience in.
nacht
1

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now