---
🚀 Fast Short URLs: Unleash the Power of URL Compression
#### What are Short URLs?
In today's digital world, long URLs often cause user frustration and clunky interfaces. These elegant short URLs transform complex web addresses into concise, manageable links, offering three major benefits:
1. ✅ Simplicity: Reduce lengthy web addresses to just 6 to 7 characters, freeing up space where it matters most.
2. ✅ User Friendliness: Short URLs are cleaner, more professional, and far easier to share across all platforms – from SMS and emails to social media and QR codes.
3. ✅ Centralized Management: Group, organize, and monitor all your links in one easy-to-navigate dashboard.
#### How Short URLs Are Implemented
At the heart of fast URL shortening are three steps:
1. Define an algorithm to map a long URL to a relatively short string.
2. Choose a storage method (like a database or NoSQL solution).
3. Implement the actual mapping for high efficiency.

This last step is the hardest part: how to map a long URL to a short string?
---
#### 🚀 Let's Break It Down
Here’s a possible solution that transforms long URLs into short ones in multiple ways.
---
##### 1. Shortening via Base62 Encoding
You might remember from math class: every number can be converted from decimal to binary or to hexadecimal. Shortening URLs by using base62 (rather than base10, base2, or base16) allows for the most compact encoding.
Algorithm Overview
We transliterate the long URL to a unique number, then use base62 encoding to condense it to a string made with lowercase letters, numbers, and no special characters — hence perfect for URL shortening.
Now, let’s define two functions: one to convert a number into a short URL, and one to decode it:
private function getShortenedURLFromID($integer, $base = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789')
{
$length = strlen($base);
$out = '';
while ($integer > 0) {
$out = $base[bcmod($integer, 62)] . $out; // Appends the character at the modulo result
$integer = bcdiv($integer, 62, 0); // Divides integer by 62
}
return $out;
}
private function getIDFromShortenedURL($string)
{
$base = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
$length = strlen($base);
$str = $string;
$id = 0;
$len = strlen($str);
for ($i = 0; $i < $len; $i++) {
$char = $str[$i];
$charPos = strpos($base, $char);
$id = $id * 62 + $charPos;
}
return $id;
}
---

##### 2. The "Long Short URL" Algorithm Example (Two-Hash Approach)
This method combines two hash functions (for minimal collision risks) to ensure uniqueness.
Process:

1. Hash the long URL using MD5 (or any other unique hash function).
2. Split the hash string into segments.
3. Process each segment to form a base62 string.
Example code:
function shortenURL($longUrl) {
$base62 = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
// Hash the URL (using SHA-256 or MD5)
$hash = hash('sha256', $longUrl, false);
$hashLen = strlen($hash);
$segments = [];
for ($i = 0; $i < $hashLen; $i += 5) {
$subHash = substr($hash, $i, 5);
$num = bin2hex($subHash);
$shortCode = '';
for ($j = 0; $j < 6 && $j < strlen($num); $j++) {
$charPos = strpos($base62, $num[$j]);
$shortCode .= $base62[$charPos];
}
$segments[] = $shortCode;
}
return implode('', $segments);
}
---
##### 3. Bas32 Algorithm (Base32 Encoding)
This method uses Base32 with 32 distinct characters, ensuring a 6-character short code with limited collisions.
function shortenBase32($longUrl) {
$base32 = 'abcdefghijklmnopqrstuvwxyz0123456789';
$hash = md5($longUrl);
$hashLen = strlen($hash);
$output = array();
for ($i = 0; $i < $hashLen; $i += 5) {
$subHash = substr($hash, $i, 5);
$num = hexdec(bin2hex($subHash));
$output[] = $base32[$num % 32];
}
$finalShortCode = '';
foreach ($output as $char) { // Ensure 6 characters per short URL
$finalShortCode .= $char;
}
return $finalShortCode;
}
---
✅ Summary
These three methods—Base62 encoding, Two-Hash Approach, and Base32 Encoding—provide robust, low-collision URL shortening. The best method depends on your project's needs regarding uniqueness, length, and performance.
---
##### 🧭 Pro-Tip
Always check if the short URL has already been created to avoid duplicates — ensuring you maintain 100% uniqueness.
---
🎉 Ready to try it out? Visit fastshorten.com or sign up for your free fastshorten account today!
立即登录