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 namespace App\Support;
Locale-aware formatting with PHP's intl extension
internationalization
encapsulation
constructor-injection
Intermediate
7 steps
php
<?php namespace App\Support;
Merging query params onto a URL in PHP
url-parsing
query-strings
immutability
Intermediate
8 steps
php
<?php class ImageUploadService {
Validating file uploads safely in PHP
file-upload
input-validation
security
Intermediate
8 steps
php
<?php namespace App\View;
Building a safe HTML escaper in PHP
security
xss
escaping
Intermediate
6 steps
java
@Target({ElementType.FIELD, ElementType.PARAMETER}) @Retention(RetentionPolicy.RUNTIME) @Constraint(validatedBy = StrongPasswordValidator.class) @Documented
Building a custom @StrongPassword validator in Spring
bean-validation
annotations
regex
Intermediate
7 steps
php
<?php namespace App\Observers;
How Eloquent observers hook lifecycle events in Laravel
observers
lifecycle-hooks
queues
Intermediate
6 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.