Can anyone point me towards a decent function for stripping out spaces and non-url-friendly characters from strings?
This is the php version I found on bitrepository
PHP Code:
<?php
function friendly_seo_string($string, $separator = '-')
{
$string = trim($string);
$string = strtolower($string); // convert to lowercase text
// Recommendation URL: http://www.webcheatsheet.com/php/regular_expressions.php
// Only space, letters, numbers and underscore are allowed
$string = trim(ereg_replace("[^ A-Za-z0-9_]", " ", $string));
/*
"t" (ASCII 9 (0x09)), a tab.
"n" (ASCII 10 (0x0A)), a new line (line feed).
"r" (ASCII 13 (0x0D)), a carriage return.
*/
$string = ereg_replace("[ tnr]+", "-", $string);
$string = str_replace(" ", $separator, $string);
$string = ereg_replace("[ -]+", "-", $string);
return $string;
}
$str = friendly_seo_string('Lorem Ipsum is simply dummy text.');
// Outputs: lorem-ipsum-is-simply-dummy-text
echo $str;
?>