Skip to content

Instantly share code, notes, and snippets.

@chanakasan
Last active August 29, 2015 14:01
Show Gist options
  • Save chanakasan/63ef42f5068bba4779b7 to your computer and use it in GitHub Desktop.
Save chanakasan/63ef42f5068bba4779b7 to your computer and use it in GitHub Desktop.
<?php
/**
* Return URL-Friendly string slug
* @param string $string
* @return string
*/
function seoUrl($string) {
//Unwanted: {UPPERCASE} ; / ? : @ & = + $ , . ! ~ * ' ( )
$string = strtolower($string);
//Strip any unwanted characters
$string = preg_replace("/[^a-z0-9_\s-]/", "", $string);
//Clean multiple dashes or whitespaces
$string = preg_replace("/[\s-]+/", " ", $string);
//Convert whitespaces and underscore to dash
$string = preg_replace("/[\s_]/", "-", $string);
return $string;
}
/** Another way */
function slugify($text)
{
// replace all non letters or digits with -
$text = preg_replace('/\W+/', '-', $text);
// trim and lowercase
$text = strtolower(trim($text, '-'));
return $text;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment