我想删除特殊符号、句点、连字符、下划线&;字符串中的数字-PHP


I want to remove special symbols, periods, hyphen, underscore & number from a string - PHP

我的要求是从字符串中删除除下划线之外的所有特殊符号。

我正在使用。。

$string = 'text-text_text+text@text(text)text&text.text*text'text/text';
$columnName = preg_replace('/[^a-zA-Z0-9_ %'[']'.'(')%&-]/s', '_', $string);

输出:

text-text_text_text_text(text)text&text.text_text_text_text

但它并没有删除句点、与号、括号和破折号。在创建这个正则表达式时,我感到很无助。请帮忙。。

当您想要删除除字母、数字和下划线之外的所有字符时,只需使用

preg_replace('/[^a-zA-Z0-9]/', '_', $string);

PREG函数中的[^...这样的表达式意味着,您希望保留以下所有字符(因此您的表达式不会(!)删除与号、括号a.s.o.

BTW:我忽略了表达式中的下划线,因为它将再次被下划线替换,所以不需要在regex 中列出它

尝试:

$string = 'text-text_text+text@text(text)text&text.text*text'text/text';
$columnName = preg_replace('/[-`~!@#$%'^&*()+={}[']''''|;:''",.><?'/]/', '_', $string);

输出:

text_text_text_text_text_text_text_text_text_text_text_text