スラッグジェネレーター
任意のテキストをURL対応のクリーンなスラッグに変換。
スラッグジェネレーターはタイトルやフレーズをURL フレンドリーなスラッグ(小文字、ハイフン区切り、特殊文字や空白なしの文字列)に変換します。例えば、"My First Blog Post!" は "my-first-blog-post" になります。
タイトルやフレーズを入力すると、ツールが即座にきれいなスラッグを生成します。変換プロセスはアクセントや発音区別符号を削除し、すべての文字を小文字に変換し、スペースと句読点をハイフンに置き換え、英数字やハイフン以外の残りの文字を削除します。
よく作られたスラッグはURLを読みやすくページのトピックに関連性があるものにすることでSEOを向上させます。検索エンジンはURLの単語を関連性シグナルとして扱います。スラッグは簡潔に保ち、可能であれば冠詞や前置詞を省略してください。
よくある質問
コード実装
import re
import unicodedata
def slugify(text: str, separator: str = "-") -> str:
"""Convert text to a URL-friendly slug."""
# Normalize unicode: decompose accented chars (é → e + combining accent)
text = unicodedata.normalize("NFKD", text)
# Encode to ASCII bytes, ignore errors (drops non-ASCII)
text = text.encode("ascii", "ignore").decode("ascii")
# Lowercase
text = text.lower()
# Replace any non-alphanumeric characters with the separator
text = re.sub(r"[^a-z0-9]+", separator, text)
# Strip leading/trailing separators
text = text.strip(separator)
return text
# Examples
print(slugify("Hello, World!")) # hello-world
print(slugify(" Multiple Spaces ")) # multiple-spaces
print(slugify("Héllo Wörld")) # hello-world
print(slugify("C++ is awesome!")) # c-is-awesome
print(slugify("Blog Post #42")) # blog-post-42
print(slugify("Hello World", "_")) # hello_world (underscore)
# For Python web frameworks like Django, use built-in slugify:
# from django.utils.text import slugify
# slugify("Hello, World!") # "hello-world"Comments & Feedback
Comments are powered by Giscus. Sign in with GitHub to leave a comment.