Automatically creating ‘short names’ / ‘slugs’ for URIs

Please note!! I have shamelessly stolen this article from my old and (even if I do say so myself) rather boring blog. So please ignore the terrible writing style! I’ll rewrite this article in the future, but until then, I present you with version 1.0 This tutorial follows on from my mod_rewrite semi-tutorial which allowed [...]

By Clinton Montague

Please note!! I have shamelessly stolen this article from my old and (even if I do say so myself) rather boring blog. So please ignore the terrible writing style! I’ll rewrite this article in the future, but until then, I present you with version 1.0

This tutorial follows on from my mod_rewrite semi-tutorial which allowed you to use friendly-looking URIs. Now I will show you how to get PHP to automatically generate a unique short name for articles which you put on your website. The following simple function does all the work for you, and it is probably simpler than you first imagined.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
function generateShortName ($name)
{
	$name = preg_replace("#([^A-Za-z0-9_-])+#", '_', $name);
	$objects = //some code to find objects
	do
	{
		$conflict = false;
		foreach ($objects as $object)
		{
			if (strcasecmp ($name, $object->shortName)==0)
			{
				$conflict = true;
				$name = '_' . $name;
				break;
			}
		}
	} while ($conflict);
	return $name;
}

What it does is change all non-url friendly charactors into an underscore, then compares this name with a list of existing names. If there is a name clash, add an underscore before the name and start again.

Voila!

Tags: , , , ,

Me on the intertubes

It wouldn't be right to have a web site, be a nerd, and not take this opportunity for a bit of shameless pluggery.

If you like all these weird and magic internet page things, you should try Coaster Nerd, Oxford Bloggers or, if you'd really like some unexplainable sourcery, is it the weekend yet.

Also you should follow me on Twitter ;)

Where to find me

I can be found fairly regularly at Oxford Geek Nights, OxTuttle and other general nerding out activities in and around Oxford.

I am also often found in my natural habitat which is anywhere that you can find an adreneline machine. I frequent English theme parks more often than is healthy, and take trips abroad to sample foreign rides.

From the aviary

Was having a decidedly crap day of development. Just got much better. It's nice when things go your way. #win 1 day ago

Subscribe

Slightlymore Posts RSS feed

Comedy disclaimer

These posts were written at the time of writing and may not still be accurate or reflect my opinions any more. In fact, they may not even be correct or representative at the time of writing. You see, when I write, I just write. I don't do that thing which they always told you to do at school and plan what you want to write first. Because of this, there will almost certainly be a load of typos, grammar errors and incorrect facts and references. If this makes you feel queasy, I apologise that you had to get to the bottom of the page to read this, and hope that some day, you'll learn to forgive me.

What's this site?

Slightlymore started off all too formally for my liking. In the depths of the archives, you'll find tutorials. However if you read the more recent posts, you'll find them a lot more playful and about fun and interesting stuff.

Annoyingly difficult to use tag cloud