Skip to content

Instantly share code, notes, and snippets.

@mazhar266
Created April 21, 2012 09:18
Show Gist options
  • Save mazhar266/fc06780fa2efe46fd2cb to your computer and use it in GitHub Desktop.
Save mazhar266/fc06780fa2efe46fd2cb to your computer and use it in GitHub Desktop.
SEO style URL making (removing unsupported)
<?php
function generateSlug($phrase, $maxLength)
{
$result = strtolower($phrase);
$result = preg_replace("/[^a-z0-9\s-]/", "", $result);
$result = trim(preg_replace("/[\s-]+/", " ", $result));
$result = trim(substr($result, 0, $maxLength));
$result = preg_replace("/\s/", "-", $result);
return $result;
}
$title = "A bunch of ()/*++\'#@$&*^!% invalid URL characters ";
echo(generateSlug($title));
// outputs
a-bunch-of-invalid-url-characters
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment