Générateur de Slugs PHP

Generate URL slugs in PHP with or without a framework, and preview the exact output before you ship it. Laravel users get Str::slug() built in — pass the language as the third argument (Str::slug($title, "-", "de")) or umlauts become "a" instead of "ae"; wire it into a model's creating event so every record slugs itself, and add ->unique() to the slug column because that index is hit on every page view. In plain PHP the standard recipe is iconv("UTF-8", "ASCII//TRANSLIT") followed by preg_replace to strip non-alphanumerics — but iconv's output depends on the server's locale settings, a classic source of "works locally, breaks in production" bugs; the code below guards against that. WordPress developers should reach for sanitize_title() instead, which handles percent-encoding and filters. Whatever you choose: never regenerate a slug when the title changes on a published URL — you will orphan every existing link unless you add a 301 redirect. Uniqueness pattern: check the database in a loop and append -2, -3… For deeper Laravel patterns (traits, Spatie Sluggable, route model binding) see our PHP & Laravel slug guide on the blog.

// Laravel
use Illuminate\Support\Str;
echo Str::slug('Merhaba Dünya', '-'); // merhaba-dunya

// Plain PHP (no framework)
function slugify(string $text): string {
    $text = iconv('UTF-8', 'ASCII//TRANSLIT', $text);
    $text = preg_replace('/[^a-zA-Z0-9]+/', '-', $text);
    return strtolower(trim($text, '-'));
}