这应该可以满足您的需求:
function clean($string) { $string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens. return preg_replace('/[^A-Za-z0-9-]/', '', $string); // Removes special chars.}用法:
echo clean('a|"bc!@£de^&$f g');将输出:
abcdef-g
编辑:
嘿,只是一个简单的问题,如何防止多个连字符彼此相邻?并将它们替换为1?
function clean($string) { $string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens. $string = preg_replace('/[^A-Za-z0-9-]/', '', $string); // Removes special chars. return preg_replace('/-+/', '-', $string); // Replaces multiple hyphens with single one.}


