I am preparing a function in PHP to automatically convert a string to be used as the file name in the URL (* .html). Although ASCII should be used to be safe, for SEO needs I need to resolve the file name in any language, but I don't want it to include punctuation except dashes (-) and underscores (_), characters like *% $ # @"'not allowed.
Spaces must be converted to dashes.
I think using Regex will be the easiest way, but I'm not sure how to handle UTF8 strings.
My ASCII functions are as follows:
function convertToPath($string)
{
$string = strtolower(trim($string));
$string = preg_replace('/[^a-z0-9-]/', '-', $string);
$string = preg_replace('/-+/', "-", $string);
return $string;
}
Thanks,
Roy.
source
share