Skip to main content

Generate URL Slugs in PHP: Str::slug() & Custom Methods

Learn to generate clean URL slugs in PHP using Laravel Str::slug() and custom functions. Handles accents and special characters.

Generating URL slugs in PHP is a fundamental skill for web developers building content-driven applications. A slug is a URL-safe, lowercase, hyphenated version of a string — essential for SEO-friendly permalinks, clean route definitions, and readable sharing URLs.

How to Use the Slug Generator Tool

  1. Visit the Slug Generator on ToolSparkr.
  2. Type or paste your text — a title, category name, or any string — into the input box.
  3. The slug appears instantly in the output field, ready to copy.
  4. Use the copied slug in your PHP code, database record, or .htaccess rewrite rule.

Generating Slugs in PHP with Laravel's Str::slug()

If you are using Laravel, the Illuminate\\Support\\Str::slug() method is the cleanest option:

<?php
use Illuminate\\Support\\Str;

$title = \"Hello, World! This is a Laravel Slug\";
$slug  = Str::slug($title);           // hello-world-this-is-a-laravel-slug

// Custom separator
$slug  = Str::slug($title, '_');      // hello_world_this_is_a_laravel_slug

// With language transliteration
$slug  = Str::slug('Héllo Wörld', '-', 'de');

Custom PHP Slug Function (No Framework)

For plain PHP or non-Laravel projects, this lightweight function handles accents, special characters, and multiple hyphens:

<?php
function slugify(string $text, string $separator = '-'): string {
    // Transliterate non-ASCII characters
    $text = transliterator_transliterate('Any-Latin; Latin-ASCII', $text);
    $text = strtolower($text);
    $text = preg_replace('/[^a-z0-9\\s-]/', '', $text);
    $text = preg_replace('/[\\s-]+/', $separator, trim($text));
    return $text;
}

echo slugify('Mon billet de blog en Français!');
// Output: mon-billet-de-blog-en-francais

Note: transliterator_transliterate() requires the PHP intl extension, which is included in most hosting environments. If unavailable, use iconv('UTF-8', 'ASCII//TRANSLIT', $text) as a fallback.

Why Use a Slug Generator?

  • Cleaner URLs — Removes query strings and encoded characters, making links shareable and readable.
  • SEO-friendly — Descriptive slugs containing keywords help search engines understand page content.
  • Routing clarity — PHP routers like FastRoute or Laravel's router match slugs predictably.
  • Database integrity — Storing a normalised slug as a unique key prevents duplicate URL collisions.

Tips and Best Practices

  • Always store the slug in the database alongside the original title — never regenerate on the fly from the title, as the title may change.
  • Add a UNIQUE index on your slug column and handle conflicts by appending -2, -3, etc.
  • Limit slug length to 200 characters in your schema; very long slugs dilute keyword relevance.
  • Validate slugs against the pattern /^[a-z0-9]+(?:-[a-z0-9]+)*$/ before inserting to prevent malformed data.

For instant slug generation without writing any code, use the free Slug Generator on ToolSparkr.

Try Slug Generator Free

Generate URL-friendly slugs from text.

Use Slug Generator →