Skip to content

Instantly share code, notes, and snippets.

@wisecrab
Last active December 21, 2015 18:45
Show Gist options
  • Select an option

  • Save wisecrab/3a2053001b45c08112c3 to your computer and use it in GitHub Desktop.

Select an option

Save wisecrab/3a2053001b45c08112c3 to your computer and use it in GitHub Desktop.
<?php
function seoFriendly($string, $separator) {
//Only accept dashes or underscores
if( $separator == "-" || $separator == "_" ) {
$separator = $separator;
} else {
$separator = "-";
}
//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