It's my blog, and I'll write what I like!
    PHP   Tidying & Formatting Postal Addresses in PHP

Tidying & Formatting Postal Addresses in PHP

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?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…

Lines 4, 5 & 6 just strip white-space from either end of the string and remove any line breaks and newlines.

Line 7 breaks the string into words on space characters and the loop from line 8 to line 18 deals with each word, one at a time.

Line 9 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 lines 11-17 deal with. Essentially, presented with a list of prefixes declared as $specials at line 11 (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 at line 12 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 lines 21-24 undo what we just did for another list of ‘specials’, which you can also add to if you wish.

Lines 26-29 just make some shorter words (are they conjunctions?) into lower case, so you and up with ‘Walton le Dale’ or ‘Stratford upon Avon’.

Then we put the text back together and return it at line 31. 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.

Possibly Related content: (Automatically generated)

  1. A Developer’s CMS
Share this
Twitter Button Facebook Button Weibo Button Tencent Button Qzone Button Google Buzz Button Reddit Button StumbleUpon Button Digg Button Orkut Button
No Comments - Leave a comment

Leave a comment

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">

Follow Me
Facebook Github GooglePlus Twitter
Archives
Popular Categories