Twitter and T.CO Shortener

If you noticed Twitter lately, that most of the short links are being replaced by Twitter’s own If you happen to tweet a shortened link using one of the lesser known URL shorteners like mine,, it will be rewritten by Twitter. It’s quite annoying if you ask me, mainly because you lose part of your own identity when you tweet. Anyways, the reasoning behind Twitter’s action is really about control, analytics and security. I get all that, but I prefer to use my own link. Dammit. Anyways. you can read more about Twitter’s action here.

Regex For URL Validation

I just wanted to share a regular expression that works for me. I was just trying to shorten a URL from Nike’s website using my own URL shortener. There was a slight problem. My script was rejecting the long URL because it was not in the right format, but there was nothing wrong with Nike’s URL. My regex was faulty. Time for a change. So, I searched online for a better regex to work with my URL shortener script. I found this code from the library that works for me.



It’s a handful. All in one line by the way.

URL Validation

Assuming we are using a HTML form to submit the long URL, here’s how to validate the URL with the help of PHP’s eregi function. The eregi function is a case insensitive regular expression matching function. If the posted URL matches the regular expression, then the statement is true. We can then shorten the URL. If the URL is invalid, we reject the URL and send out a message saying the URL is not in the correct format.

$urlregex = “^(http|https|ftp)\://([a-zA-Z0-9\.\-]+(\:[a-zA-Z0-9\.&%\$\-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|localhost|([a-zA-Z0-9\-]+\.)*[a-zA-Z0-9\-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|me|name|pro|aero|coop|museum|[a-zA-Z]{2}))(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\?\’\\\+&%\$#\=~_\-]+))*$”;

if (eregi($urlregex, $_POST[‘url’])) {
//  url is valid. shorten long url
} else {
// reject. url is invalid

Matches |’s | | |’s |

One last thing, You can add more TLDs and country code to the regular expression. I added the .me domain since one of my domains is a .me.

Generating PHP Random Keys

There are many ways of generating random keys in PHP. Random keys are essential in applications where you need unique identifiers that are not necessarily in sequential order. A prime example of where to use this script would be is when you need to create unique key for a URL shortener script.

For example: a user supplies a long URL. The script generates a random key based a predetermined length and returns a short URL based on the random key. So, here are  a couple of PHP random scripts that you might find useful. They both generate 6 random characters each time they are called.

Example 1

We create a function called rand_str(). We set the characters and generate a random key with a length of 6 characters. We make sure the same two characters don’t appear next to each other.

function rand_str($length = 6, $chars = ‘ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890?) {
  $chars_length = (strlen($chars)1); // Length of character list
  $string = $chars{rand(0, $chars_length)}; // Start our string
  for ($i = 1; $i < $length; $i = strlen($string)) { // Generate random string
    $r = $chars{rand(0, $chars_length)}; // Grab a random character from our list
    if ($r != $string{$i1}) { 
      $string .=  $r; // Make sure the same two characters don’t appear next to each other
    echo $string; // Return the string

Example 2

This is example is essentially the same as above, but a bit more succinct. It generates a 6 character random string, but it doesn’t have a consecutive character feature like above.

for ($s =, $i = 0, $z = strlen($a = ’0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ’)-1; $i != 6; $x = rand(0,$z), $s .= $a{$x}, $i++);
$key = $s;
echo $key;

There you have it. Two scripts for generating random strings based on a predetermined length. It’s perfect for applications that need to generate unique and random keys.

There’s a good possibility a “swear” word could be generated by random chance. To avoid such words, simply take out the vowels from your character list.

Start Your Own URL Shortener

Are you thinking about running your own URL shortener? If you own a blog or a website, you should consider. There are many valid reasons why you should own your own url shortener.

#1 Branding

Having your own url shortener promotes your website and your blog. Your visitors will see your unique link. When you tweet or share a link with someone, the link you share is yours. It’s unique. It carries your own brand. The short url from,,,, or from any other 3rd party service works great, but it’s not yours. My personal url shortener is Every article on my blog has a short url. Check out the short url at the end of this article. You will see a Tweet This button below.

#2 Independence

There are over 100 url shortener services on the internet. Most of these 3rd party url shortener services are free. There is, however, no guarantee that they will keep their services up and running for eternity. You, however, have control of your own website and your own links. You can control how url shortener behaves. You’ll have the option to determine which hashing methods to use, from base36 to base64, from sequential to random. You call your own shots.

#3 Statistics

Another reason for having your own url shortener is you want to keep track of your statistics. Not every url shortener service has statistics. Every time you’ve send an email with a link, you want to know if your recipient has clicked on your link. If you sent a tweet, you would like to know how many people have clicked on your links. Statistics informs you of the effectiveness of your tweets.

#4 Security

Some people don’t trust short urls since they are essentially cloaked links. There is no telling where a cloaked link will take you. Cloaked links can lead someone to an untrusted or a phishing site. You could easily acquire a virus, malware, adware or spam when you land on an untrusted site. If you run your own website or blog and you use your own short urls, you establish trust with your own readers. On top of that, you are promoting your own brand.

How do I make one?

Ok. convinced. How do I get started? How do I make one? The good news is you can get it here. My url shortener script is available for only $49.99. See features below:

Try the Demo


  • Requires PHP, MySQL, .htaccess
  • Works with any domain
  • 1 configuration file.
  • 1 second install
  • Base36 encoding
  • Can be modified to use Base64
  • Sequential links from 10000 to zzzzz
  • Hashing can be set to random.
  • Supports up to 2 billion links
  • Statistics: display number of clicks
  • Statistics: display last 10 links
  • Search for partial keywords
  • API: simple integration with 3rd party software
  • API format: create.php?url=http://longurl…..
  • URL format of http://short.url/1z35sf
  • Seamless integration with WordPress and Twitter
  • WordPress: works great with Tweet This plugin
  • Creates short url link when publishing a WordPress article
  • Price includes installation, support and updates

Try the Demo

Purchase for only $49.99

Google and Facebook Launch URL Shorteners

Practically, anyone who tweet nowadays uses a URL shortener service. was the first one out of the gate as far as I can recall. Quickly followed by a slew of services joining the fray. The list starts from,,, and on and on. Here’s a comprehensive list – well, at the time it was written.

Not listed is my own personal URL shortener at Launched about two months ago and used mostly for the personal branding of my blog – as opposed to using a third party service. Every article on my blog has a short URL, including this article. In addition, I also use for Twitter and email.

Now, Facebook and Google have started their own service. Facebook launched today, while Google announced their own called Did anyone see these two coming? I’m a bit surprised. They both caught me off guard. Google’s service will focus on stability, security and speed. I’m sure they will do a great job.

Well, I’m proud to say that I got my service up before Google and Facebook ever did. I beat them by two months. That’s one accomplishment I can brag about. I know it’s not much, but it’s worth something to me. Well, it’s a great way to start the week.


If wasn’t short enough for you, try It’s a new domain owned by It’s just 3 characters including the top level domain. That’s pretty short.  It runs the same URL shortening service currently being offered by

I have recently started my own personal URL shortener: It works great, too. It uses base36 numerical system and generates sequential characters instead of random. It’s simpler. If you ever tweet, URL shorteners are a godsend.

By the way, I’m running a Twitter-based website on It uses a PHP class which pulls Twitter information via APIs.