David Preston

Musings of an old bloke.



From the Archives

Posted about 3 weeks ago (2 November 2014 1:28 pm) by David Preston

I was clicking through some folders of old (very old, actually) stuff, and found and old website that I had at the beginning of the century. It was a blog in all but name, even though I referred to it at the time as my Angling Diary. The files I found ran from November 2000 to June 2001. I used to fish a lot around then!

I thought it would be a bit of fun to reproduce it here, even with some of the tiny, horrible, pixelated photos, for which I apologise.

You can find all of the posts in the category From my old Angling Diary.

Join the Discussion

Categories: Uncategorised

Southport Air Show 2014

Posted about 4 weeks ago (27 October 2014 6:43 pm) by David Preston

The Red ArrowsThe Red Arrows

I had a day out at the Southport Air Show a few weeks ago. It was held over a weekend, the Saturday suffering from pretty gloomy weather with low cloud. I went on the Sunday, when conditions were much more suitable.

Here are a few photos.

There are 6 more images on this post's page.

Join the Discussion

Categories: Out & About

dpreston.com Now Powered by ProcessWire

Posted about 1 month ago (24 October 2014 6:45 pm) by David Preston

After years of neglect, during which time server upgrades and Wordpress awkwardness almost rendered this blog completely inoperable, I finally made the leap to change its backend to ProcessWire.

I first discovered ProcessWire a couple of years ago, and have built several sites with it since, mostly small ones but the odd biggie.

The following ProcessWire modules are in use -

And kinda surprisingly, that's all.

The front end of the site uses a couple of ready made CSS files -

The daft idea originally was to avoid JS as much as possible, but it snuck in all over the place eventually.

It's going to be permanently 'in beta', and used as a testbed where appropriate fun.

Let me know what you think in the comments section.

Join the Discussion

Categories: Web Development

Tags: Processwire

Prime Minister Follows Parody Darth Vader On Twitter

Posted about 3 years ago (25 May 2012 12:00 am) by David Preston

Twitter screen shot

I’m not sure if this is good, bad, cool, uncool or just plain weird. The official Twitter account of the British Prime Minister follows a spoof Darth Vader account!

Well, I suppose Cameron is something of a parody of a Prime Minister anyway, so why not?

Join the Discussion

Categories: Uncategorised

Tidying & Formatting Postal Addresses in PHP

Posted about 3 years ago (8 May 2012 2:50 pm) by David Preston

A couple of times in the course of my job, I've needed to tidy-up user input of postal addresses, usually when importing from an external database into a web application or taking input from a web form. It looks horrible when everything is in upper or lower case. PHP has strtoupper(), strtolower() and ucasewords() functions, but none of these does just what I needed. Googling found a few attempts to solve the same problem, and I've probably pinched bits of code from various places. If I've pinched some of yours, let me know and I'll acknowledge your contribution. Hence the function below.

<?php
function address_tidy($text) {
    $text = trim($text);
    $text = str_replace("\\r", "", $text);
    $text = str_replace("\\n", "", $text);
    $words = explode(" ", $text);
    foreach ($words as $word) {
        $word = ucfirst(strtolower($word));
        //Specials like Mac, Mc etc
        $specials = array("Mac", "Mc", "O'");
        foreach ($specials as $special) {
            $pos = stripos($word, $special);
            if (($pos !== false) && ($pos == 0)) {
                $parts = explode($special, $word);
                $word = $special . ucfirst($parts[1]);
            }
        }
        //...but not for some words that begin with "Mac"
        // (make your own mind up about Macintosh, Maclure & Maclaren)
        $specials = array("macken", "macclesfield", "machynlleth");
        if (in_array(strtolower($word), $specials)) {
            $word = ucfirst(strtolower($word));
        }
        //Let"s go lower case on some words
        $specials = array("de", "la", "le", "on", "of", "and", "under", "upon");
        if (in_array(strtolower($word), $specials)) {
            $word = strtolower($word);
        }
    }
    return implode(" ", $words);
}
?>

Here’s a quick run-down of what it does when you feed it a string…

The first 3 lines just strip white-space from either end of the string and remove any line breaks and newlines.

We then use explode() to break the string into words on space characters and the foreach() loop deals with each word, one at a time.

The first line inside the loop does most of the work, converting the word first to lower case and then capitalising it, which is great for the vast majority of words, but what about Scottish and Irish names? That’s what the next few lines deal with. Essentially, presented with a list of prefixes declared as $specials (which you can add to if needed), we then find any words that begin with those prefixes and capitalise the fragment of word following. Example – the word being processed arrives as ‘Macdonald’, and the code goes through each of the ‘specials’ to see if our word starts with the prefix. It does in the case of ‘Mac’, so we capitalise ‘donald’ and put the two fragments back together as ‘MacDonald’.

But that messes up ‘Macclesfield’, making it ‘MacClesfield’, so the next few lines undo what we just did for another list of ‘specials’, which you can also add to if you wish.

Then we just make some shorter words (are they conjunctions?) into lower case, so you end up with ‘Walton le Dale’ or ‘Stratford upon Avon’.

Then we put the text back together and return it. Easy. Help yourself if you find this function useful. Any links back or other acknowledgements would be more than welcome, but aren't necessary.

Leave a comment if you find any bugs, or you think you can improve my code.

Join the Discussion

Categories: Web Development

Tags: PHP