这是我的正则表达式,用于删除特殊字符。
但我不想从文本中删除空格和@
。
function clean($string) {
$string = str_replace(' ', ' ', $string); // Replaces all spaces with hyphens.
return preg_replace('/[^A-Za-z0-9'-]/', '', $string); // Removes special chars.
}
上面的代码不允许有空格,也不知道如何允许@持久存在。
将模式更改为不替换它们,然后:'/[^A-Za-z0-9 @'-]/'
不过,要确保清洁剂仍然足够干净。
看起来您应该阅读一篇关于perl兼容正则表达式的小文本。