Skip to content

Instantly share code, notes, and snippets.

@joparara
Created May 20, 2024 22:08
Show Gist options
  • Save joparara/bf2db21e570118165f7c6e686515c9b9 to your computer and use it in GitHub Desktop.
Save joparara/bf2db21e570118165f7c6e686515c9b9 to your computer and use it in GitHub Desktop.
sluggify helper
<?php
function slugify($text) {
$text = preg_replace('~[^\pL\d]+~u', '-', $text);
$text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
$text = preg_replace('~[^-\w]+~', '', $text);
$text = preg_replace('~-+~', '-', $text);
$text = strtolower($text);
$text = trim($text, " \t\n\r\0\x0B-");
if (empty($text)) {
return 'n-a';
}
return $text;
}
@joparara
Copy link
Author

Or, use Illuminate\Support\Str; Str::slug($input, $separator = '-')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment