I was using the below function to clean post titles to create a GUID link and unique name, but while I thought it was working iit doesn't seem to actually be doing anything useful, for instance this:
"l'sm,-$~~*&dfs$%*£!"!/"
Is meant to be the sanitized title from "l'sm, $~~*&DFS$%*£!"!/".
I don't quite understand the preg_replace function or how they derive the patterns, how would I go about eliminating these characters as I'm guessing that using a name like that in a URL will not end well.
PHP Code://SANITIZE A TITLE FOR USE
function cleanTitle($title, $optional='', $type='') {
$title = strip_tags(trim($title));
$title = preg_replace('/&.+?;/', '', $title);
if ( '' === $title || false === $title )
$title = $optional;
if ($type === 'cleanname') {
$title = strtolower($title);
$title = preg_replace('/\s+/', '-', $title);
}
return $title;
}



Reply With Quote

Thank you for your help Cups I will go investigate Slugs.

Bookmarks