Skip to content

Instantly share code, notes, and snippets.

@gabrielef
Last active June 23, 2017 19:48
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save gabrielef/11244087 to your computer and use it in GitHub Desktop.
Save gabrielef/11244087 to your computer and use it in GitHub Desktop.
Convert title to seo friendly url
<?php
class SanitizeUrl {
public static function slug($string, $space="-") {
$string = utf8_encode($string);
if (function_exists('iconv')) {
$string = iconv('UTF-8', 'ASCII//TRANSLIT', $string);
}
$string = preg_replace("/[^a-zA-Z0-9 \-]/", "", $string);
$string = trim(preg_replace("/\\s+/", " ", $string));
$string = strtolower($string);
$string = str_replace(" ", $space, $string);
return $string;
}
}
$title = 'Thi is a test string with some "strange" chars ò à ù...';
echo SanitizeUrl::slug($title);
//this will output:
//thi-is-a-test-string-with-some-strange-chars-o-a-u
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment