Skip to content

Instantly share code, notes, and snippets.

@kobus1998
Created October 22, 2019 13:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kobus1998/1858d940698baaa1f6fc22d06d381b77 to your computer and use it in GitHub Desktop.
Save kobus1998/1858d940698baaa1f6fc22d06d381b77 to your computer and use it in GitHub Desktop.
slugify for url, id, var etc
<?php
function slugify($sVal, $sDelimiter = '-')
{
$sVal = preg_replace("/[^a-zA-Z0-9_-]/", $sDelimiter, $sVal);
$sVal = preg_replace("/$sDelimiter{2,}/", $sDelimiter, $sVal);
return trim($sVal, $sDelimiter);
}
echo slugify("my company name ~ !!!!!") . "\n"; // my-company-name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment