php 如何删除非字母数字字符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/659025/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:24:01  来源:igfitidea点击:

How to remove non-alphanumeric characters?

phpregexstring

提问by zuk1

I need to remove all characters from a string which aren't in a-z A-Z 0-9set or are not spaces.

我需要从字符串中删除所有不在a-z A-Z 0-9集合中或不是空格的字符。

Does anyone have a function to do this?

有没有人有这样做的功能?

回答by Chad Birch

Sounds like you almost knew what you wanted to do already, you basically defined it as a regex.

听起来您几乎已经知道自己想要做什么,您基本上将其定义为正则表达式。

preg_replace("/[^A-Za-z0-9 ]/", '', $string);

回答by voondo

For unicode characters, it is :

对于 unicode 字符,它是:

preg_replace("/[^[:alnum:][:space:]]/u", '', $string);

回答by raspi

Regular expressionis your answer.

正则表达式就是你的答案。

$str = preg_replace('/[^a-z\d ]/i', '', $str);
  • The istands for case insensitive.
  • ^means, does not start with.
  • \dmatches any digit.
  • a-zmatches all characters between aand z. Because of the iparameter you don't have to specify a-zand A-Z.
  • After \dthere is a space, so spaces are allowed in this regex.
  • i代表不区分大小写。
  • ^意味着,不开始。
  • \d匹配任何数字。
  • a-z匹配a和之间的所有字符z。由于该i参数,您不必指定a-zand A-Z
  • 后面\d有空格,所以这个正则表达式中允许有空格。

回答by Alex Stephens

here's a really simple regex for that:

这是一个非常简单的正则表达式:

\W|_

and used as you need it (with a forward /slash delimiter).

并根据需要使用(使用正/斜杠分隔符)。

preg_replace("/\W|_/", '', $string);

Test it here with this great tool that explains what the regex is doing:

用这个很好的工具在这里测试它,它解释了正则表达式的作用:

http://www.regexr.com/

http://www.regexr.com/

回答by Jonathon

If you need to support other languages, instead of the typical A-Z, you can use the following:

如果您需要支持其他语言,而不是典型的 AZ,您可以使用以下内容:

preg_replace('/[^\p{L}\p{N} ]+/', '', $string);
  • [^\p{L}\p{N} ]defines a negated(It will match a character that is notdefined) character class of:
    • \p{L}: a letter from anylanguage.
    • \p{N}: a numeric character in anyscript.
    • : a space character.
  • +greedilymatches the character class between 1 and unlimitedtimes.
  • [^\p{L}\p{N} ]定义了一个否定(它将匹配在一个字符定义)字符类的:
    • \p{L}: 来自任何语言的一封信。
    • \p{N}:任何脚本中的数字字符。
    • : 一个空格字符。
  • +贪婪地匹配 1次和无限次之间的字符类。

This will preserve letters and numbers from other languages and scripts as well as A-Z:

这将保留来自其他语言和脚本以及 AZ 的字母和数字:

preg_replace('/[^\p{L}\p{N} ]+/', '', 'hello-world'); // helloworld
preg_replace('/[^\p{L}\p{N} ]+/', '', 'abc@~#123-+=???'); // abc123???
preg_replace('/[^\p{L}\p{N} ]+/', '', '你好世界!@£$%^&*()'); // 你好世界


Note:This is a very old, but still relevant question. I am answering purely to provide supplementary information that may be useful to future visitors.

注意:这是一个非常古老但仍然相关的问题。我的回答纯粹是为了提供可能对未来访问者有用的补充信息。

回答by Intacto

[\W_]+

?

?

$string = preg_replace("/[\W_]+/u", '', $string);

It select all not A-Z, a-z, 0-9 and delete it.

它选择所有不是AZ,az,0-9并删除它。

See example here: https://regexr.com/3h1rj

请参阅此处的示例:https: //regexr.com/3h1rj

回答by TOZ

preg_replace("/\W+/", '', $string)

You can test it here : http://regexr.com/

你可以在这里测试:http: //regexr.com/

回答by ssi-anik

I was looking for the answer too and my intention was to clean every non-alpha and there shouldn't have more than one space.
So, I modified Alex's answer to this, and this is working for me preg_replace('/[^a-z|\s+]+/i', ' ', $name)
The regex above turned sy8ed sirajul7_islamto sy ed sirajul islam
Explanation: regex will check NOT ANYfrom a to z in case insensitiveway or more than one white spaces, and it will be converted to a single space.

我也在寻找答案,我的目的是清理每个非 alpha 并且不应该有超过一个空间。
所以,我修改亚历克斯的答案,这是为我工作 preg_replace('/[^a-z|\s+]+/i', ' ', $name)
的正则表达式以上转向sy8ed sirajul7_islamsy ed sirajul islam
说明:正则表达式将检查又没从A到Z的情况下,不区分大小写的方式或一个以上的空格,它会被转换为单一空间。

回答by zekel

You can split the string into characters and filter it.

您可以将字符串拆分为字符并对其进行过滤。

<?php 

function filter_alphanum($string) {
    $characters = str_split($string);
    $alphaNumeric = array_filter($characters,"ctype_alnum");
    return join($alphaNumeric);
}

$res = filter_alphanum("a!bc!#123");
print_r($res); // abc123

?>