Last active
April 13, 2022 10:48
-
-
Save garcon/fd50d7ea81df93b67540134547789880 to your computer and use it in GitHub Desktop.
Change any string into SEO friendly URL slug
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// example code | |
setlocale(LC_CTYPE, 'cs_CZ'); | |
$str = "Běloučký kůň úpěl ďábelské ódy $ ' 1234"; | |
preg_replace('/(?<=-)-+/', '', // Replace all duplicase hyphens with one only | |
preg_replace( '/[^-a-z0-9]/', '', // Strip all non-alfanumeric characters or hyphens | |
strtr( // Replace spaces with hyphens | |
strtolower( // Transform to lowercase | |
iconv('UTF-8', 'ASCII//TRANSLIT', $str) // Transliterate to ASCII | |
), | |
[" " => "-"] | |
) | |
) | |
) | |
; | |
// Expected output: | |
// beloucky-kun-upel-dabelske-ody-1234 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment