I currently use this piece of code to reduce a given text to a valid “tagging” format (only lowercase, a-z and minus allowed) by removing/replacing invalid characters
$zip_filename = strtolower($original);
$zip_filename = preg_replace("/[^a-zA-Z\-]/g", '-', $zip_filename); //replace invalid chars
$zip_filename = preg_replace("/-+/g", '-', $zip_filename); // reduce consecutive minus to only one
$zip_filename = preg_replace("/^-/g", '', $zip_filename); // removing leading minus
$zip_filename = preg_replace("/-$/g", '', $zip_filename); // remove trailing minus
Any hints on how to put at least the regex into a single one?
Thanks for any advice!
Explanation:
A-Zis useless since it should be lower case+after right bracket will replace one or more consecutive invalid charstrimwith second parameter – character to trim form beginning and end will speed up the code\-frompreg_replacewill also take car of hyphens between invalid chars / multiple consecutive hyphens, replacing them to single one.