Skip to content

Instantly share code, notes, and snippets.

@abhshkdz
Created November 2, 2013 08:45
Show Gist options
  • Save abhshkdz/7276887 to your computer and use it in GitHub Desktop.
Save abhshkdz/7276887 to your computer and use it in GitHub Desktop.
Generating a Url slug in PHP
<?php
function generateUrlSlug($string, $maxlen=0)
{
$string = trim(preg_replace('/[^a-z0-9]+/', '-', strtolower($string)), '-');
if ($maxlen && strlen($string) > $maxlen) {
$string = substr($string, 0, $maxlen);
$pos = strrpos($string, '-');
if ($pos > 0) {
$string = substr($string, 0, $pos);
}
}
return $string;
}
$string = "Generate URL slug";
print generateUrlSlug($string);
?>
@Samirdatainflow
Copy link

If you are working with PHP- Codeigniter framework and you want to Generate URL slug in Codeigniter then follow this link
https://datainflow.com/generate-url-slug-codeigniter/

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