The SitePoint Forums have moved.

You can now find them here.
This forum is now closed to new posts, but you can browse existing content.
You can find out more information about the move and how to open a new account (if necessary) here.
If you get stuck you can get support by emailing forums@sitepoint.com

If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

This is for a website I am developing that deals with various different affiliates, so there will be lots of different substrings to check (one for each affiliate partner) to determine $sitename.

My question is, how would I accomplish the same thing with a SWITCH statement (checking $url for many different substrings and giving $sitename a different value based on whether or not the substring is found).

Thanks in advance!

P.S. If this looks trivial, I'm not actually using $sitename in the actual program, I just used it hypothetically because it's easier to describe than the actual process my function accomplishes, and if I learn how to accomplish the above process with a SWITCH statement, my question is answered and I can integrate it into my program.

Long answer: Can't be done. At least not in this fashion. If your strings are always in the same format and thus your target substring is always in the same place, you could extract the substring and then use a switch statement to figure out what processing your function needs to do. If this is the case, we need to know the format of your string.

Didn't think so... I spent about 20 minutes looking it up but to no avail so I thought I'd try asking the experts.

Since each URL is likely to have a different length of characters and will have various other substrings after the domain portion of the URL, there is no set pattern to the strings that are being analysed. I guess I'll have to use IF statements.

Variable-length domain names is no problem ("Behold! The power of REGEX!"). Provided we know what comes before (e.g. "http://") and what comes after (maybe "/"?), we can extract the domain name with no problem. If php_daemon's suggestion doesn't work for you (and I can't see any reason it won't), post a couple of example strings and we'll find your REGEX.

When dealing with a URL like http://my.earthlink.net, kromey's function outputs "my.earthlink.net" as the domain, while yours correctly outputs "earthlink.net".

However, with URLs with no "www." or subdomain (i.e. "http://blahblahblah.com"), you would have to display $prefix, $root and $suffix to get the desired result in your function.

Both functions could be tweaked. If I learn regular expressions, I'm sure subdomain prefixes could be handled with kromey's function, and with an IF statement comparing $suffix to the string "www." could get around the problem in yours... I think!

Thanks again to both of you, if you have any further suggestions/solutions that would be great.

To be honest, I think I could get away with having subdomain prefixes in the URLs, since it will probably be uniform across the whole affiliate's website in terms of product pages and all I'd have to do is include the subdomain prefix in the switch statement case.

However, I'd be interested to see the code of how it would work removing the subdomain as well, just out of curiosity. Any recommended reading on regular expressions? Or should I just go RTFM?