Created
November 2, 2013 08:45
-
-
Save abhshkdz/7276887 to your computer and use it in GitHub Desktop.
Generating a Url slug in PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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/