Skip to content

Instantly share code, notes, and snippets.

@omurphy27
Created July 27, 2016 04:24
Show Gist options
  • Save omurphy27/ceb3ebb704c49e262dd6607c72b77fe8 to your computer and use it in GitHub Desktop.
Save omurphy27/ceb3ebb704c49e262dd6607c72b77fe8 to your computer and use it in GitHub Desktop.
PHP - create seo friendly slug from string
<?php // credit: http://stackoverflow.com/questions/11330480/strip-php-variable-replace-white-spaces-with-dashes
function seoUrl($string) {
//Lower case everything
$string = strtolower($string);
//Make alphanumeric (removes all other characters)
$string = preg_replace("/[^a-z0-9_\s-]/", "", $string);
//Clean up multiple dashes or whitespaces
$string = preg_replace("/[\s-]+/", " ", $string);
//Convert whitespaces and underscore to dash
$string = preg_replace("/[\s_]/", "-", $string);
return $string;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment