creating a slug from title field
Hi
(Happy New Year!)
I've modified the Dreamweaver snippet slightly to produce a php lowercase slug:
<?php
function create_slug($string, $charset = 'utf-8'){
$string = htmlentities($string, ENT_NOQUOTES, $charset, false); // convert accented characters to entities
// strip unwanted parts of entities to leave unaccented character
$string = preg_replace('~&([A-za-z])(?:acute|cedil|caron|circ|grave|orn|ring|slash|th|tilde|uml);~', '\1', $string);
$string = preg_replace('~&([A-za-z]{2})(?:lig);~', '\1', $string);
$string = strtolower($string);
$string = preg_replace('~&[^;]+;~', '', $string); // remove other entities
return preg_replace('~[\s!*\'();:@&=+$,/?%#[\]]+~', '-', $string); // replace spaces and illegal characters with hyphens
}
echo create_slug("Something here! NO CAPITALS")."<br>";// returns 'something-here-no-capitals'
?>
(The 'Something here...' text is the database title field value)
After adding the record, the user is redirected to the page where the slug is generated in a form field - this page updates on load, then on to the 'real' destination, but it would be even better if the slug could be added when the page is created or updated.
How might I achieve this?
Many thanks in anticipation
Chris