- substr: documentation ( source)
- strtolower: documentation ( source)
<?php
function remapInternationalCharToAscii($c)
{
return $c;
}
function sluggify($title)
{
if ($title === null) return "";
$maxlen = 80;
$len = strlen($title);
$prevdash = false;
$slug = "";
for ($i = 0; $i < $len; $i++) {
$c = $title[$i];
if (($c >= 'a' && $c <= 'z') || ($c >= '0' && $c <= '9')) {
$slug .= $c;
$prevdash = false;
}
else if ($c >= 'A' && $c <= 'Z')
{
echo 'hi!';
$slug .= strtolower($c);
$prevdash = false;
}
else if ($c == ' ' || $c == ',' || $c == '.' || $c == '/' ||
$c == '\\' || $c == '-' || $c == '_' || $c == '=')
{
if (!$prevdash && strlen($slug) > 0)
{
$slug .= '-';
$prevdash = true;
}
}
else if ((int) $c >= 128)
{
$prevlen = strlen($slug);
$slug .= remapInternationalCharToAscii($c);
if ($prevlen != strlen($slug)) $prevdash = false;
}
if ($i == $maxlen) break;
}
if ($prevdash) {
return substr($slug, 0, strlen($slug) - 1);
}
return $slug;
}
echo sluggify('Hello World');