An incredibly silly function: leetHost().

The following throwaway function will convert the first letter of every word comprising a FQDN to an uppercase letter. I did it mostly out of boredom, but secondly to abuse ternary operators.

Here’s the function:

function leetHost($hostName=FALSE) { // What have I been smoking? No, this is not meant to be legible $hostName = (preg_match(’/^\d+\.\d+\.\d+\.\d+/’, $hostName)) ? strtolower(@gethostbyaddr($hostName)) : ((is_string($hostName) && isset($hostName)) ? strtolower($hostName) : ((isset($_SERVER[‘SERVER_NAME’]) ? $_SERVER[‘SERVER_NAME’] : @gethostbyaddr(“127.0.0.1”)))); $hostArray = explode(”.”, $hostName); for ($i = 0; $i < count($hostArray); $i++) { $hostArray[$i] = ucfirst($hostArray[$i]); }} return(implode(”.”, $hostArray)); }

The whole of the logic is in the nested tenary operations above. Everything else is just, er, tearing it down, and rebuilding it.

Here’s what those crazy nested ternary operators do: First I test if the given string matches all numerics (likely an IP address, and if it is a string). If so, then convert it to a hostname, and ensure that this is lower case. (Note that this does not actually test for a fully-valid IP address. Any decimals are accepted, so it’s logic would believe that 999.999.999.999 was a valid IP address, however, this is sufficient for this simple little function.)

Secondly, if the hostname is actually set, and it is a string, convert it to lower case, and assume it is valid.

Lastly, if there is no hostname set, default to the webserver’s configured address. If that fails, which it shouldn’t, use localhost, and look up the hostname for that, just to be pedantic.

Pshew.

Now, break the hostname into an array using the periods as delimiters. Convert the first letter of each word to an uppercase letter, and stuff it all back into a single string, replacing the periods. Return that string.

A usage example:

echo “1: ” . leetHost(“203.194.209.85”) . “
2: ” . leetHost() . “
3: ” . leetHost(“66.102.7.99”) . “
”;

For my localized testings, I obtain the following: 1: System447.Com
2: Www.Holwegner.Com
3: 66.102.7.99

Note that this simple function doesn’t have issues with hostnames that don’t resolve due to the preceeding ’@’ before the gethostbyname. Sometimes it’s just easiest to fail gracefully.

Is this function entirely worthless? Probably. It’s still better than the other function I was thinking of, which would convert the entire hostname to uppercase, then selectively replace the vowels with lowercase. Perhaps for a future, configurable version of leetHost() ;)