Created
January 11, 2020 17:31
-
-
Save fhferreira/2162a213fb2a9b2a6430de153a3bd1f2 to your computer and use it in GitHub Desktop.
Slug php
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 | |
function slugify($text) | |
{ | |
// replace non letter or digits by - | |
$text = preg_replace('~[^\pL\d]+~u', '-', $text); | |
// transliterate | |
$text = iconv('utf-8', 'us-ascii//TRANSLIT', $text); | |
// remove unwanted characters | |
$text = preg_replace('~[^-\w]+~', '', $text); | |
// trim | |
$text = trim($text, '-'); | |
// remove duplicate - | |
$text = preg_replace('~-+~', '-', $text); | |
// lowercase | |
$text = strtolower($text); | |
if (empty($text)) { | |
return 'n-a'; | |
} | |
return $text; | |
} | |
//using | |
echo slugify('Pioneering Change Conference: Night of Worship'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment