Derek Gathright | Scribbles & Bits

Archive for October 2009

Often times you’ll come across the need to generate a random string. When you search for something like “PHP random string function” which will probably just be cut & pasted into your code, you’ll typically find something like this function:

function genRandomString($length = 10) {
    $characters = ‘0123456789abcdefghijklmnopqrstuvwxyz’;
    $string =;
    for ($p = 0; $p < $length; $p++) {
        $string .= $characters[mt_rand(0, strlen($characters))];
    }
 
    return $string;
}

While that’s great and all, don’t you just feel like there’s some better, simpler way to generate a random string? Preferably something done all in one line, so you don’t even need a function inside your code?

There is a way. Actually, there are lots of ways. There are numerous functions in PHP that can be used to generate a string of random characters, and the one I typically use here is something you’ve probably used quite a bit… md5().

If you aren’t familiar with it, MD5 is a commonly used hashing technique that can be used to generate a unique 32-character “fingerprint” of a string. While it has been shown to have some security holes and not be collision proof, that isn’t really a concern the majority of the time you need to generate a random string. So, we’ll go ahead and use it here.

echo MD5(); // PHP Warning:  md5() expects at least 1 parameter, 0 given

So, we need to pass in something, anything, but try to be unique. microtime() is a good choice.

echo MD5(microtime()); // 5959e4c551ee3a91d89449437b681ead

Great, now we have a random 32 character string. But what if you only want a slice of that, like 5 characters? substr() is your answer.

echo substr(MD5(microtime(), 0, 5); // 5959e

If you are concerned about security and making this string as random as possible, just shuffle it with str_shuffle().

echo substr(str_shuffle(MD5(microtime())), 0, 5); // 6c468

Now wasn’t that easy?

No tags

Marshall Kirkpatrick of ReadWriteWeb had an interesting post today recapping an interview with Google’s CEO Eric Schmidt at last week’s Gartner Symposium. In a short, 6 minute segment out of the full 45 minute interview (video below), Schmidt hinted at what the future of the internet will bring.

Via RWW: Google’s Eric Schmidt on What the Web Will Look Like in 5 Years

  • Five years from now the internet will be dominated by Chinese-language content.
  • Today’s teenagers are the model of how the web will work in five years – they jump from app to app to app seamlessly.
  • Five years is a factor of ten in Moore’s Law, meaning that computers will be capable of far more by that time than they are today.
  • Within five years there will be broadband well above 100MB in performance – and distribution distinctions between TV, radio and the web will go away.
  • “We’re starting to make significant money off of Youtube”, content will move towards more video.
  • “Real time information is just as valuable as all the other information, we want it included in our search results.”
  • There are many companies beyond Twitter and Facebook doing real time.
  • “We can index real-time info now – but how do we rank it?”
  • It’s because of this fundamental shift towards user-generated information that people will listen more to other people than to traditional sources. Learning how to rank that “is the great challenge of the age.” Schmidt believes Google can solve that problem.

It’s hard to argue with any of those, really. The one I’m looking forward to most of all is the convergence of media platforms as the lines between TV, Radio, and Web become more and more blurry. As those lines become blurred, more innovation will occur between the gaps and the old-time revenue models based mostly on distribution begin to fall apart.

No tags

If you are doing some more advanced JavaScript applications, you’ll likely run into the problem at one point or another when you need to dynamically generate the name of a function you wish to call. This is the equivalent of PHP’s call_user_func().

Here’s how you can do it.

// Define some vars
var foo = "hello";
var bar = "world";
var function_name = "say_" + foo + bar;
 
// Since its name is being dynamically generated, always ensure your function actually exists
if (typeof(window[function_name]) === "function")
{
	window[function_name](" World!");
}
else
{
	throw("Error.  Function " + function_name + " does not exist.");
}
 
function say_helloworld(the_word)
{
	alert("Hello " + the_word);
}
 
// Browser will alert "Hello World!"

When you think about it, it makes a lot of sense considering all global objects in JavaScript are actually properties of the “window” object. Take a look at this example to understand that concept a bit more.

var foo = "apple";
 
function foobar()
{
	var foo = "orange";
	alert(window["foo"]); // alerts "apple"
	alert(foo); // alerts "orange"
	alert(window["foobar"]); // alerts the foobar function
}
 
foobar();

Here’s another example of when using a string to call a function could come in handy. This time we’re calling the property of an object we’ve created.

function Person()
{
	this.message = "Hello World!";
	this.say_hello = function(to)
	{
		alert(this.message);
	}
}
 
var Derek = new Person();
function_name = "say_hello";
Derek[function_name]();

See how it looks pretty much the same as the method above using a global function?

Hope that shed some light on your problem.

Find it!

Theme Design by devolux.org