- substr: documentation ( source)
- stripos: documentation ( source)
- strtolower: documentation ( source)
<?php
function slugify($title) {
$output = '';
$valid_characters = 'abcdefghijklmnopqrstuvwxyz1234567890';
$len = strlen($title);
for ($i = 0;$i < $len;$i++) {
if (stripos($valid_characters, $title[$i]) !== FALSE) {
// Letters and numbers are valid
$output .= strtolower($title[$i]);
} elseif ($i > 0 && $title[$i] !== '\'' && stripos($valid_characters, $title[$i - 1]) !== FALSE) {
// Other characters are turned into dashes, but never two
// in a row.
$output .= '-';
}
}
if (substr($output, -1, 1) === '-') {
return substr($output, 0, -1);
} else {
return $output;
}
}
echo slugify('This is not a love song').PHP_EOL;
echo slugify('There\'s a lady who is sure').PHP_EOL;
echo slugify('All that is gold does not glitter ').PHP_EOL;