Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php preg_replace可删除除破折号、字母、数字、空格和下划线以外的所有字符_Php_Regex - Fatal编程技术网

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它可以处理空格。空格是我正在处理的问题的一个组成部分,接受的答案可以处理空格。它。你写道我需要删除字符串中除破折号以外的所有字符字母、数字、空格和下划线。进行了一些编辑,请检查。将
+
量词附加到类中一个或多个。