Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.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 如何从字符串中去除非字母、空格和数字字符?_Php_Regex - Fatal编程技术网

Php 如何从字符串中去除非字母、空格和数字字符?

Php 如何从字符串中去除非字母、空格和数字字符?,php,regex,Php,Regex,如何使用php和正则表达式从字符串中去除所有非字母、空格和所有数字 我试过这个: $input = "Hello - World 12"; $output = preg_replace("/[^a-zA-Z0-9\s]/", "", $input); 想要的输出:HelloWorld使用以下表达式: $answer = preg_replace("/[^A-Za-z]/", '', $input); 这将删除不属于以下内容的任何字符: A-Z a-z 要删除任何空白,请执行以下操作: $s

如何使用php和正则表达式从字符串中去除所有
非字母
空格
和所有
数字

我试过这个:

$input = "Hello - World 12";
$output = preg_replace("/[^a-zA-Z0-9\s]/", "", $input);
想要的输出:
HelloWorld

使用以下表达式:

$answer = preg_replace("/[^A-Za-z]/", '', $input);
这将删除不属于以下内容的任何字符:

A-Z
a-z
要删除任何空白,请执行以下操作:

$string = preg_replace('/\s+/', '', $answer);
使用以下表达式:

$answer = preg_replace("/[^A-Za-z]/", '', $input);
这将删除不属于以下内容的任何字符:

A-Z
a-z
要删除任何空白,请执行以下操作:

$string = preg_replace('/\s+/', '', $answer);
你可以用

$output = preg_replace("~[\W0-9_]~", "", $input);
你可以用

$output = preg_replace("~[\W0-9_]~", "", $input);

从正则表达式中删除0-9和\s,如下所示:

$input = "Hello - World 12";
$output = preg_replace("/[^a-zA-Z]/", "", $input);

现在,您正在检查不是(
^
)小写a-z或大写a-z的每个字符。然后,您将不使用任何内容替换该字符。“

从正则表达式中删除0-9和\s,如下所示:

$input = "Hello - World 12";
$output = preg_replace("/[^a-zA-Z]/", "", $input);

现在,您正在检查不是(
^
)小写a-z或大写a-z的每个字符。如果您不使用任何内容替换该字符,请检查。“

我还想删除所有数字字符。答案已更新,请检查。谢谢,但它不会删除空格,是吗?我还想删除所有数字字符。答案已更新,请检查。谢谢,但它不会删除空格,有可能吗?谢谢,很好。谢谢,很好。