Foundations, Knowledge Builder, Lessons Learned, Snippets, Site News

Knowledge Builder: Sort collection of objects using any member variable

As I go further down the rabbit hole that is web-development, and break away from the path of pre-built libraries and frameworks, I just keep learning more.

A few weeks ago I finally got around to creating my own collection class - for the uninitiated, a collection is a way of creating something that behaves like an array, but with the added advantage of having inheritable and extensible methods, like an object.

Create a URL slug from any string

Okay, so automically generating a friendly URL for a blog post or forum post from their titles doesn't really take a lot - but this snippet might make it easier for you:

function make_slug($orig, $length = 0)
{
  $slug = preg_replace('/[^a-zA-Z0-9]/', '-', $orig);
  $slug = preg_replace('/\-\-+/', '-', $slug);
  $slug = strtolower($slug);
 
  if ($length > 0) {
    // limits the length of the slug
    $slug = substr($slug, 0, $length);
  }
 
  if (strrpos($slug, '-') == strlen($slug) - 1) {
    // removes any ending -
    $slug = substr($slug, -1);
  }

Snippet: function round_e()

I recently helped a poster at HTML Forums who wanted to be able to reformat floating point numbers in PHP.

The problem is that some of these numbers were in exponential format. There was also a requirement not to round a number like 0.004 to 0, although the 'precision' was to be constant for all uses of this function in the script.

8.8458e-119    = 8.84e-119
1.06542e-52    = 1.07e-52
2.68e-36       = 2.68e-36
2.91405e-35    = 2.92e-35
0.0190644      = 0.01
0.0205511      = 0.02
0.004          = 0.004

Snippet: function intToRoman()

A fun little function that will produce the Roman Numeral representation for any number from 1-3999.

The reason for the limitation is because the Romans used barred letters for the number 5000 and larger, and the representation of 4000 would be MV, which is hard to represent in a simple ASCII string (maybe as an exercise to the reader, the function can be extended?).

function intToRoman($number)
{
    if (!is_numeric($number)) {
        echo "Input is not a number\r\n\r\n";
        return;
    }
 

New Theme

A few weeks ago I asked my friend Sofee at Heart Shaped Designs if she would mind designing a site theme for RandomtTweak.

After a couple of weeks of swapping thoughts (from me) and designs (from her), I was handed a pretty smart looking final design and the component images and font specifications for me to use in creating the final HTML/CSS design. I just hope that Sofee will forgive what I've done with her design...

Theming Drupal

Now on Twitter

RandomTweak is now on Twitter - http://twitter.com/randomtweak

New Author Joins RandomTweak

JediLlama - a veteran C/C++ developer and Qt guru has joined RandomTweak to share his wealth of know-how and tips for developing desktop applications.

JediLlama has been developing and supporting complex I-CMS and network applications for many years know, and I'm sure he will have lots to share in the future.

Snippet: Generate a Time-Based Key

I recently helped another developer looking for a way to only allow access to one site from an iframe on another. Ideally, this would be done with the HTTP_REFERRER header, but this isn't always set by the browser, and so would prevent too many people from seeing the content properly.

My solution was simply to use a time-based key that could be generated on the site containing the iframe and tested on the remote server before allowing access to the content.

Switch to Drupal

Notice a change?

Well, not only has the layout changed for the site, the backend system has been migrated from WordPress to the Drupal Content Management Framework.

WordPress is a pretty good system for publishing simple blogs, but I found that extending it to support more complex posts and site configuration wasn't all that easy. Doing a bit of research, and after playing around with a couple of other systems, I picked out Drupal.

Lessons Learned: Branch ahead of change (SVN)

When I set up a branched development on SVN the other week, I knew that it would cause some problems down the line. It was my own fault really, and I deserved to get bitten for it just like I did today. Actually, the problem extends to a bit before that, and comes down to a simple case of lack of planning.

One of my ongoing projects has two developers working on different areas of it – but for the most part we’ve been able to schedule development and delivery such that we’ve been able to work out of a single development tree in the SVN repository.