Archive for the ‘Useful Stuff’ Category

PHP function to generate a slug for SEO URLs

March 7th, 2010 | No Comments »

Increase SEO by using your article name/product name/category name/whatever in your URLs instead of a non-descriptive database ID.

function slug($string) {
    $string = trim($string);
    $string = strtolower($string);
    $string = str_replace('&', 'and', $string);
    $string = preg_replace('/[^a-z0-9-]/', '-', $string);
    $string = preg_replace('/-+/', "-", $string);
    return $string;
}
Categories: Useful Stuff

Force SSL by loading the same page via HTTPS

December 26th, 2009 | No Comments »

Just a simple one, I place this in a common file, and then call it at the top of each page that I require to be secured by SSL.

function force_ssl() {
    if ($_SERVER['HTTPS'] != 'on') {
        header("Location: https://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}");
        exit();
    }
}
Tags: | Categories: Useful Stuff

MySQL string escaping in PHP

December 26th, 2009 | No Comments »

Simple function to prepare user inputted data for inserting into a MySQL database. It simply checks if stripslashes() should be called, and then escapes the string. Basic, but handy.

function mysql_escape($string) {
    if (get_magic_quotes_gpc() == 1) {
        $string = stripslashes($string);
    }
    $string = mysql_real_escape_string($string);
    return $string;
}
Tags: , | Categories: Useful Stuff

The most useful PHP function you’ll ever use!

December 26th, 2009 | No Comments »

This is the first thing I add to any PHP based project and I couldn’t live without it..

Most people will probably understand and appreciate what this does, but for those that don’t, it simply displays the contents of an array in a very human readable way by using the <pre> tag to preserve the spacing of the print_r() command.

// Debug Array
function da($array) {
    echo "<pre>\n";
    print_r($array);
    echo "</pre>\n";
}

Enjoy!

Tags: | Categories: Useful Stuff

Bash alias to search file contents

December 26th, 2009 | No Comments »

This simple Bash alias will search the contents of every file in the current directory (and sub-directories) and return the file names. Very handy when trying to work out which file generated which HTML in large PHP based applications or when trying to find every last instance of a particular string in a bunch of config files.

# Search In Files
sif() {
    grep -EiIrl "$*" ./*
}

Either use the grep command by itself, replacing the $* with your search string for a one time use, or add this to your .bashrc file to be able to search by typing: sif search string

Tags: | Categories: Useful Stuff

My config files

December 26th, 2009 | No Comments »

For those that are interested and for my own reference…

(more…)

Tags: | Categories: Useful Stuff