
September 18, 2008
I love coming across old code *cough* garbage *cough*
The purpose of this was to take a title, and create something url safe and in the fashion a client was wanting. The first function was old code, so don’t blame me.
function cleanUrl($url)
{
$find = array('/--/', '/ - /','/ /', '/!/', '/"/', "/'/", '/--/');
$replace = array('-', '-', '-', '', '', '', '-');
$result = strtr($url, "`~@#$%^&*()_=+|[]{};:,./<>?", "---------------------------");
return strtolower(preg_replace($find, $replace, strip_tags(stripslashes(trim($result)))));
}
Isn’t it horrible?
I rewrote the function to…
function cleanUrl($url)
{
$url = strtolower($url);
$url = preg_replace("/[^a-z0-9\s+]/", '', $url);
$url = preg_replace("/[\s]{1,}/", '-', $url);
return $url;
}
That makes me happier.

June 29, 2008
Ewerl is a new URL rewritting site with cool features.
Here’s a simple function to return the Ewerl url. It’s only being used for the main url, but could easily be adjusted to return an array. I just didn’t have a need for it in my case at this time.
/**
* Returns an Ewerl url if available.
* Otherwise the original that was passed in.
* Returns false if..
* 1. JSON extension isn't installed.
* 2. No url was entered.
* 3. No response from server.
*
* @todo Add URL validation.
*
* @param string $url
* @return string URL, or false.
* @author Casey Wilson
*/
function getEwerl($url) {
/**
* Verify JSON extension is loaded.
*/
if (!extension_loaded('JSON')) return false;
/**
* Url wasn't passed
*/
if (!$url) return false;
/**
* Assign / Prep vars.
*/
$postUrl = 'http://ewerl.com/api/v1/make/url=';
$urlEncoded = base64_encode($url);
$response = file_get_contents($postUrl . $urlEncoded);
if (!$response) {
return $url;
}
/**
* Should have returned a JSON object.
*/
$jsonObj = json_decode($response);
/**
* If either Status not set, or status is false, return original url.
*/
if (!isset($jsonObj->status) || $jsonObj->status == 'FALSE') {
return $url;
}
return $jsonObj->url_main;
}

June 21, 2008
I needed to transform some long urls into tiny ones :p
Figured I’d post something up here that may be useful to someone else.
Code was updated.
/**
* Get TinyUrl for $url
*
* @param string $url Must be a valid url
*/
function getTinyUrl($url) {
if (! $url) {
return false;
}
if (strlen ( $url ) < 30) {
return $url;
}
$response = file_get_contents ( 'http://tinyurl.com/api-create.php?url=' . $url );
return ($response) ? $response : $url;
}