从字符串中删除非字母数字字符

发布于 2024-08-27 20:47:41 字数 52 浏览 12 评论 0原文

我有一个 PHP 字符串,我希望它与正则表达式 [A-Za-Z0-9] 匹配。我该怎么做?

I have a string in PHP and I want it to match the regex [A-Za-Z0-9]. How can I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

万劫不复 2024-09-03 20:47:41

我假设您的意思是,在正则表达式中使用 az 而不是 aZ,但您可以使用 preg_replace

$new_string = preg_replace("/[^a-zA-Z0-9\s]/", "", $string);

它采用模式 ([a-zA-Z0 -9]),替换("")和主题($string)并返回新字符串($new_string

I am assuming you meant, a-z instead of a-Z, inside of your regex, but you can use preg_replace

$new_string = preg_replace("/[^a-zA-Z0-9\s]/", "", $string);

It takes as arguments the pattern ([a-zA-Z0-9]), replacement ("") and the subject ($string) and returns the new string ($new_string)

梦情居士 2024-09-03 20:47:41
$string = preg_replace('/[^a-zA-Z0-9]/', '', $string);
$string = preg_replace('/[^a-zA-Z0-9]/', '', $string);
你げ笑在眉眼 2024-09-03 20:47:41

\W[^a-Z0-9_] 的快捷方式。可能不是很有帮助,因为它也允许下划线,但我想让你知道。

\W is a shortcut for [^a-Z0-9_]. May not be extremely helpful as it allows underscores too, but thought I'd let you know.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文