How to Slugify Text in JavaScript, Python, and PHP
Slugifying text—converting a human-readable string into a URL-safe slug—is a common task in web development. Whether you’re building a blog, e-commerce site, or API, you’ll need to generate clean URL slugs from titles and other text. This guide shows you how to do it in the three most popular server-side languages.
Don’t want to write code? Use our Slugify Online tool to convert text to slugs instantly in your browser.
JavaScript / Node.js
Using the slugify Library
The most popular approach in JavaScript is the slugify npm package:
npm install slugifyconst slugify = require('slugify');
const title = "Café Menu — Special Édition!";
const slug = slugify(title, {
lower: true,
strict: true,
trim: true
});
console.log(slug);
// Output: cafe-menu-special-editionOptions explained:
lower: true— converts to lowercasestrict: true— strips all characters that aren’t letters, numbers, or the separatortrim: true— removes leading/trailing separators
Manual Implementation
If you prefer not to add a dependency, here’s a minimal slugify function:
function slugify(text) {
return text
.normalize('NFKD')
.replace(/[\u0300-\u036f]/g, '')
.toLowerCase()
.trim()
.replace(/[^a-z0-9\s-]/g, '')
.replace(/[\s_]+/g, '-')
.replace(/-+/g, '-')
.replace(/^-|-$/g, '');
}This handles accented characters via Unicode normalization, converts to lowercase, removes special characters, and collapses whitespace into single hyphens.
Python
Using python-slugify
The standard library for Python slugification is python-slugify:
pip install python-slugifyfrom slugify import slugify
title = "Café Menu — Special Édition!"
slug = slugify(title)
print(slug)
# Output: cafe-menu-special-editionpython-slugify uses the text-unidecode library under the hood for transliteration, which handles a wide range of Unicode characters including CJK.
Django’s Built-in slugify
If you’re using Django, it has a built-in slugify utility:
from django.utils.text import slugify
slug = slugify("My Blog Post Title")
# Output: my-blog-post-titleNote: Django’s slugifydoesn’t handle transliteration by default. For accented characters, combine it with unidecode.
PHP
Laravel’s Str::slug()
Laravel provides a clean slug helper:
use Illuminate\Support\Str;
$slug = Str::slug("Café Menu — Special Édition!");
// Output: cafe-menu-special-editionStr::slug() handles transliteration, lowercasing, and special character removal in one call.
Manual Implementation
Without a framework, here’s a PHP slugify function:
function slugify(string $text): string {
$text = transliterator_transliterate(
'Any-Latin; Latin-ASCII; Lower()',
$text
);
$text = preg_replace('/[^a-z0-9\s-]/', '', $text);
$text = preg_replace('/[\s_]+/', '-', $text);
$text = preg_replace('/-+/', '-', $text);
return trim($text, '-');
}This uses PHP’s intl extension for transliteration. Make sure the intl extension is enabled in your php.ini.
Comparison Table
| Feature | JS (slugify) | Python (python-slugify) | PHP (Laravel) |
|---|---|---|---|
| Transliteration | Basic Latin | Full Unicode | Full Unicode |
| Custom separator | Yes | Yes | Yes |
| Max length | No | Yes | No (manual) |
| CJK support | No | Yes | Partial |
When to Use a Library vs. Online Tool
Use a librarywhen you need to generate slugs programmatically at runtime—for example, auto-generating a URL when a user creates a blog post.
Use an online tool when you need a one-off slug or want to quickly check what a title looks like as a URL. Our Text to Slug converter supports custom separators, max length, and transliteration—all the options you’d configure in a library, without writing any code.