Creating a Slugify Function in Different Programming Languages: A Beginner's Guide
In web development, it's common to convert a string into a URL-friendly format, which is called "slugification." For example, transforming the title of a blog post like "Hello World!" into "hello-world" helps make the URL both readable and SEO-friendly. This process is known as "slugifying" a string.
In this article, we'll explore how to create a simple slugify
function in some of the most popular programming languages, such as PHP, Go, Rust, Python, C, C++, and others. Whether you're a beginner in coding or an experienced developer curious about different languages, you'll find this guide helpful as we break down the slugification process step by step.
What Is Slugification?
Slugification involves taking a string of text, typically a title or name, and converting it into a string suitable for use in a URL. This usually includes converting to lowercase, replacing spaces with hyphens, and removing any special characters.
For example, the string:
Hello World!
Would become:
hello-world
Slugifying in JavaScript (Node.js)
function slugify(str) {
return str
.toLowerCase() // Convert the string to lowercase
.trim() // Remove whitespace from both ends
.replace(/[^\w\s-]/g, '') // Remove all non-word characters (letters, digits, etc.)
.replace(/\s+/g, '-') // Replace spaces with hyphens
.replace(/-+/g, '-'); // Replace multiple hyphens with a single hyphen
}
console.log(slugify('Hello World!')); // Output: hello-world
Slugifying in PHP
function slugify($string) {
// Convert string to lowercase
$string = strtolower($string);
// Remove non-word characters (letters, numbers, spaces, and hyphens)
$string = preg_replace('/[^\w\s-]/', '', $string);
// Replace spaces and multiple hyphens with a single hyphen
$string = preg_replace('/\s+/', '-', $string);
$string = preg_replace('/-+/', '-', $string);
// Trim hyphens from the beginning and end
return trim($string, '-');
}
echo slugify('Hello World!'); // Output: hello-world
Slugifying in Go
package main
import (
"fmt"
"regexp"
"strings"
)
func slugify(s string) string {
// Convert to lowercase
s = strings.ToLower(s)
// Remove non-alphanumeric characters (except for spaces and hyphens)
re := regexp.MustCompile(`[^\w\s-]`)
s = re.ReplaceAllString(s, "")
// Replace spaces with hyphens
s = strings.ReplaceAll(s, " ", "-")
// Remove consecutive hyphens
re = regexp.MustCompile(`-+`)
s = re.ReplaceAllString(s, "-")
// Trim hyphens
return strings.Trim(s, "-")
}
func main() {
fmt.Println(slugify("Hello World!")) // Output: hello-world
}
Slugifying in Rust
fn slugify(input: &str) -> String {
let slug = input
.to_lowercase() // Convert to lowercase
.replace(|c: char| !c.is_alphanumeric() && c != ' ', "") // Remove non-alphanumeric characters
.replace(' ', "-") // Replace spaces with hyphens
.replace("--", "-") // Replace consecutive hyphens
.trim_matches('-') // Trim leading and trailing hyphens
.to_string();
slug
}
fn main() {
let slug = slugify("Hello World!");
println!("{}", slug); // Output: hello-world
}
Slugifying in TypeScript
function slugify(str: string): string {
return str
.toLowerCase() // Convert to lowercase
.trim() // Trim spaces from both ends
.replace(/[^\w\s-]/g, '') // Remove all non-word characters
.replace(/\s+/g, '-') // Replace spaces with hyphens
.replace(/-+/g, '-'); // Replace multiple hyphens with one
}
console.log(slugify('Hello World!')); // Output: hello-world
Slugifying in Python
import re
def slugify(string):
# Convert to lowercase
string = string.lower()
# Remove non-word characters (except for spaces and hyphens)
string = re.sub(r'[^\w\s-]', '', string)
# Replace spaces with hyphens
string = re.sub(r'\s+', '-', string)
# Remove multiple hyphens
string = re.sub(r'-+', '-', string)
# Trim leading and trailing hyphens
return string.strip('-')
print(slugify("Hello World!")) # Output: hello-world
Slugifying in C
C doesn't have built-in string manipulation libraries as rich as higher-level languages, so you'll need to handle the string processing manually:
#include <stdio.h>
#include <ctype.h>
#include <string.h>
void slugify(char *str) {
int i = 0, j = 0;
int hyphen = 0;
// Convert string to lowercase and process it
while (str[i]) {
if (isalnum(str[i])) {
str[j++] = tolower(str[i]);
hyphen = 0;
} else if (isspace(str[i]) && !hyphen) {
str[j++] = '-';
hyphen = 1;
}
i++;
}
// Remove trailing hyphen, if any
if (j > 0 && str[j - 1] == '-') {
j--;
}
str[j] = '\0';
}
int main() {
char input[] = "Hello World!";
slugify(input);
printf("%s\n", input); // Output: hello-world
return 0;
}
Slugifying in C++
C++ has the Standard Library that makes string manipulation easier than in C:
#include <iostream>
#include <algorithm>
#include <cctype>
std::string slugify(const std::string& input) {
std::string result;
bool lastHyphen = false;
for (char ch : input) {
if (std::isalnum(ch)) {
result += std::tolower(ch);
lastHyphen = false;
} else if (std::isspace(ch) && !lastHyphen) {
result += '-';
lastHyphen = true;
}
}
// Remove trailing hyphen, if any
if (!result.empty() && result.back() == '-') {
result.pop_back();
}
return result;
}
int main() {
std::string input = "Hello World!";
std::string slug = slugify(input);
std::cout << slug << std::endl; // Output: hello-world
return 0;
}
Slugifying in Ruby
In Ruby, string manipulation tasks like slugifying are quite simple, thanks to its powerful built-in methods and the use of regular expressions.
def slugify(string)
# Convert to lowercase
string = string.downcase
# Remove non-word characters (except spaces and hyphens)
string = string.gsub(/[^\w\s-]/, '')
# Replace spaces with hyphens
string = string.gsub(/\s+/, '-')
# Remove multiple hyphens and trim leading/trailing hyphens
string = string.gsub(/-+/, '-').strip
end
puts slugify("Hello World!") # Output: hello-world
Understanding the Common Pattern
While the syntax may differ between programming languages, the general process for slugifying a string remains the same:
- Convert the string to lowercase.
- Remove any characters that aren’t letters, numbers, spaces, or hyphens.
- Replace spaces with hyphens.
- Remove any consecutive hyphens.
- Trim any leading or trailing hyphens.
Each language handles these steps in its own way, but the logic remains consistent. Python, with its rich libraries, handles slugification concisely, while C requires a more hands-on approach. C++ strikes a balance, offering more convenience while still maintaining control over the string manipulation process.
Comments ()