Text Slugify (URL)
Normalize text into URL-friendly slugs with support for lowercase conversion, custom separators, and customizable stopword removal.
Why Use Slugify?
🔍 SEO Optimization
Keywords in URLs help search engines understand page content and improve rankings. For example, example.com/blog/how-to-learn-javascript is more effective than example.com/blog/123.
👁️ Readability & Sharing
Users can instantly understand the content from the URL. Slugs are more shareable on social media and easier to remember or type manually.
💻 System Compatibility
Avoid errors caused by special characters in filenames or URLs. Ensures cross-platform compatibility (Windows/Linux/macOS) and prevents encoding issues.
🗄️ Database-Friendly
Use as unique identifiers (e.g., usernames, tags) to reduce SQL injection risks and improve indexing and query performance.
What is Slugify?
A slug is a shortened, standardized version of text used as a URL, filename, or identifier. Common processing includes standardizing case, removing punctuation, and joining words with separators.
- ASCII Priority: Remove accents and symbols, keep only alphanumeric characters and spaces
- Unicode Compatible: Normalize most language characters using NFKD before processing
- URL-Safe: Output contains only letters, numbers, and separators—ready for direct use in paths
Frequently Asked Questions
Q: How are Chinese characters handled?
A: By default, diacritics are removed and pinyin letters are retained. Pure Chinese text may become empty; it's recommended to manually convert to pinyin before slugifying, or use a Chinese pinyin conversion tool.
Q: Why is my result empty?
A: Your input may consist entirely of punctuation, symbols, or spaces, or all words may have been removed by stopword filtering. Try disabling the stopword option or adjusting your input.
Q: Should I use - or _ as the separator?
A: For SEO, use - (hyphen), as Google treats it as a space; _ (underscore) is treated as a concatenation symbol, which hinders word separation. For filenames, either is acceptable.
Q: Is there a length limit for slugs?
A: There's no technical limit, but it's recommended to keep slugs under 50 characters for better URL display and SEO. Excessively long slugs may be truncated by search engines.
Best Practices
Recommended Practices
- ✓ Keep it short (recommended < 50 characters)
- ✓ Avoid special characters; use only letters, numbers, and separators
- ✓ Use lowercase to avoid case-sensitivity issues
- ✓ Remove stopwords to improve semantic density
Avoid These Practices
- ✗ Do not include sensitive information (e.g., IDs, emails, passwords)
- ✗ Do not use special characters (e.g., @#$%^&*)
- ✗ Do not retain spaces or consecutive separators
- ✗ Do not repeat the same word
Technical Details
Uses NFKD decomposition + removal of combining diacritics (\p{M}), converting Café to Cafe. Supports most Latin-based characters.
Based on common English words (a/an/the/and/or/of/to/in/on/for/at/by/with); customizable. Chinese stopwords require separate handling.
Requires ES6+ and Unicode regex support (\p{...}). Supported in modern browsers: Chrome 64+, Firefox 78+, Safari 11.1+.
How to Generate a Slug Through a Programming Language?
JavaScript
function slugify(text) {
return text
.toLowerCase()
.normalize("NFKD")
.replace(/[\u0300-\u036f]/g, "")
.replace(/[^\w\s-]/g, "")
.trim()
.replace(/[\s_-]+/g, "-")
.replace(/^-+|-+$/g, "");
}
PHP
function slugify($text) {
$text = mb_strtolower($text);
$text = iconv("UTF-8", "ASCII//TRANSLIT", $text);
$text = preg_replace("/[^\w\s-]/", "", $text);
$text = preg_replace("/[\s_-]+/", "-", $text);
return trim($text, "-");
}
Python
import re
import unicodedata
def slugify(text):
text = text.lower()
text = unicodedata.normalize("NFKD", text)
text = text.encode("ascii", "ignore").decode("ascii")
text = re.sub(r"[^\w\s-]", "", text)
text = re.sub(r"[\s_-]+", "-", text)
return text.strip("-")
Go
import (
"regexp"
"strings"
"golang.org/x/text/unicode/norm"
)
func Slugify(text string) string {
text = strings.ToLower(text)
text = norm.NFKD.String(text)
re := regexp.MustCompile(`[^\w\s-]`)
text = re.ReplaceAllString(text, "")
re = regexp.MustCompile(`[\s_-]+`)
text = re.ReplaceAllString(text, "-")
return strings.Trim(text, "-")
}
Ruby
require "unicode"
def slugify(text)
text = text.downcase
text = Unicode.nfkd(text).gsub(/[^\x00-\x7F]/, "")
text = text.gsub(/[^\w\s-]/, "")
text = text.gsub(/[\s_-]+/, "-")
text.strip.gsub(/^-+|-+$/, "")
end
Java
import java.text.Normalizer;
public static String slugify(String text) {
text = text.toLowerCase();
text = Normalizer.normalize(text, Normalizer.Form.NFKD);
text = text.replaceAll("[^\\w\\s-]", "");
text = text.replaceAll("[\\s_-]+", "-");
return text.replaceAll("^-+|-+$", "");
}