Skip to content

Instantly share code, notes, and snippets.

@aswebdev
Last active December 13, 2018 03:56
Show Gist options
  • Save aswebdev/cf9c68a30e82635b42e5 to your computer and use it in GitHub Desktop.
Save aswebdev/cf9c68a30e82635b42e5 to your computer and use it in GitHub Desktop.
Creates an SEO Friendly URL from a string
// SEO Friendly Filter Function
function seoUrl($string) {
$string = trim($string); // Trim String
$string = strtolower($string); //Unwanted: {UPPERCASE} ; / ? : @ & = + $ , . ! ~ * ' ( )
$string = preg_replace("/[^a-z0-9_\s-]/", "", $string); //Strip any unwanted characters
$string = preg_replace("/[\s-]+/", " ", $string); // Clean multiple dashes or whitespaces
$string = preg_replace("/[\s_]/", "-", $string); //Convert whitespaces and underscore to dash
return $string;
}
@FarrisFahad
Copy link

Thanks a lot :)

@gueff
Copy link

gueff commented Feb 10, 2018

another Solution using PHP's iconv with TRANSLIT:
https://blog.ueffing.net/post/2016/03/14/creating-seo-friendly-url/

@marceloagil
Copy link

Hmm there's a problem when words have accents, so I found this solution.

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