INCLUDE_DATA
Browsing the archives for the refactoring tag.


Old code.

Code, php

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.

1 Comment


"));