Php preg_replace可删除除破折号、字母、数字、空格和下划线以外的所有字符
我需要删除字符串中除破折号、字母、数字、空格和下划线以外的所有字符 各种答案非常接近(,等等),但通常不包括破折号Php preg_replace可删除除破折号、字母、数字、空格和下划线以外的所有字符,php,regex,Php,Regex,我需要删除字符串中除破折号、字母、数字、空格和下划线以外的所有字符 各种答案非常接近(,等等),但通常不包括破折号 非常感谢您的帮助。您可能需要以下帮助: $new = preg_replace('/[^ \w-]/', '', $old); 说明: [^ \w-] Match any single character NOT present in the list below «[^ \w-]» The literal character “ ” « » A “word ch
非常感谢您的帮助。您可能需要以下帮助:
$new = preg_replace('/[^ \w-]/', '', $old);
说明:
[^ \w-]
Match any single character NOT present in the list below «[^ \w-]»
The literal character “ ” « »
A “word character” (Unicode; any letter or ideograph, any number, underscore) «\w»
The literal character “-” «-»
您可以执行以下操作:
$string = ';")<br>kk23how nowbrowncow_-asdjhajsdhasdk32423ASDASD*%$@#!^ASDASDSA4sadfasd_-?!';
$new_string = preg_replace('/[^ \w-]/', '', $string);
echo $new_string;
$string=';”
kk23how nowbrowncow-asdjhajsdhasdk32423ASDASD*$@!^ASDASDSA4sadfasd-;
$new_string=preg_replace('/[^\w-]/',''.$string);
echo$new_字符串;
表示[^]
是单词字符\w
[a-Za-z0-9\
逐字匹配连字符-
preg\u replace('/[^\s\w-]/','''.$old);
如果你使用Unicode,'/[^\s\w\p{M}\p{Pd}]/u'
(其中\p{Pd}
是任何破折号).@WiktorStribiżew我已经用了几个小时了,它可以处理空格。好的,但是这个问题与答案不匹配。@WiktorStribiżew它可以处理空格。空格是我正在处理的问题的一个组成部分,接受的答案可以处理空格。它。你写道我需要删除字符串中除破折号以外的所有字符字母、数字、空格和下划线。进行了一些编辑,请检查。将+
量词附加到类中一个或多个。