php
39 lines · 8 steps
Building a URL slug from any title in PHP
A step-by-step regex pipeline turns arbitrary, accented text into a clean, lowercase URL slug.
Explained by
highlit
1<?php
2
3namespace App\Support;
4
5final class Slugifier
6{
7 public function make(string $title, string $separator = '-'): string
8 {
9 $title = $this->transliterate($title);
10 $title = mb_strtolower($title, 'UTF-8');
11
12 $flip = $separator === '-' ? '_' : '-';
13 $title = preg_replace('![' . preg_quote($flip, '!') . ']+!u', $separator, $title);
14
15 $title = str_replace('@', $separator . 'at' . $separator, $title);
16
17 $title = preg_replace('![^' . preg_quote($separator, '!') . '\pL\pN\s]+!u', '', $title);
18
19 $title = preg_replace('![' . preg_quote($separator, '!') . '\s]+!u', $separator, $title);
20
21 return trim($title, $separator);
22 }
23
24 private function transliterate(string $value): string
25 {
26 if (function_exists('transliterator_transliterate')) {
27 $transliterated = transliterator_transliterate(
28 'Any-Latin; Latin-ASCII; [\u0080-\u7fff] remove',
29 $value
30 );
31
32 if ($transliterated !== false) {
33 return $transliterated;
34 }
35 }
36
37 return iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $value) ?: $value;
38 }
39}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Normalizing accents to ASCII before slugifying keeps URLs stable across languages.
- 2Layering small regex passes — flip, strip, collapse, trim — is clearer than one monster pattern.
- 3Preferring the intl extension with an iconv fallback makes transliteration robust across environments.
Related explainers
php
<?php class NameParser {
Parsing a full name into components in PHP
string-parsing
arrays
normalization
Intermediate
8 steps
php
<?php namespace App\Services\Checkout;
Validating coupons with Laravel's Pipeline
pipeline
chain of responsibility
transactions
Intermediate
7 steps
ruby
class UserAgentParser BROWSERS = [ [/Edg\/([\d.]+)/, "Edge"], [/OPR\/([\d.]+)/, "Opera"],
Parsing user-agent strings in Ruby
regex
pattern-matching
lookup-tables
Intermediate
8 steps
php
<?php namespace App\Services;
How a password strength validator works in PHP
validation
regular-expressions
data-driven
Intermediate
8 steps
php
<?php namespace App\Services;
Building a cached daily leaderboard in Laravel
caching
aggregation
eager-loading
Intermediate
9 steps
php
<?php final class RotatingFileLogger {
How a rotating file logger works in PHP
logging
file-rotation
io
Intermediate
9 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/building-a-url-slug-from-any-title-in-php-explained-php-1d1a/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.