从字符串中删除非字母数字字符(包括 ß、Ê 等) [英] Remove non-alphanumeric characters (including ß, Ê, etc.) from a string

查看:77
本文介绍了从字符串中删除非字母数字字符(包括 ß、Ê 等)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种简单的方法可以从 PHP 中的字符串中删除所有非字母数字字符,而无需在正则表达式函数中单独列出它们?

Is there an easy way to remove all non alphanumeric characters from a string in PHP that wouldn't require listing them all individually in a regex function?

我过去一直在使用 preg_replace("/[^a-zA-Z0-9\s\'\-]/", "", $my_string); 过去,但是这个过滤器删除重要字符,如 ÀÈÌÒÙß 等.

I have been using preg_replace("/[^a-zA-Z0-9\s\'\-]/", "", $my_string);in the past but this filters out important characters like ÀÈÌÒÙß etc.

我需要清理名称字段,因此不需要货币和数学字符/符号.

I need to sanitize a name field, so monetary and mathematical characters/symbols are not needed.

推荐答案

像这样:

preg_replace('/[^\p{L}\p{N}\s]/u', '', $my_string);

正如 arnaud576875 已经提到的,您应该知道,当像我一样使用 u 修饰符时,模式被视为 UTF-8.相应手册页的相关摘录:

As arnaud576875 already mentioned, you should be aware that the pattern is treated as UTF-8 when using the u modifier like I did. Relevant excerpt of the appropriate manual page:

你(PCRE8)

此修饰符开启 PCRE 的附加功能,即与 Perl 不兼容.模式字符串被视为 UTF-8.这修饰符可从 PHP 4.1.0 或更高版本在 Unix 和 PHP 中获得4.2.3 在 win32 上.自 PHP 起检查模式的 UTF-8 有效性4.3.5.

This modifier turns on additional functionality of PCRE that is incompatible with Perl. Pattern strings are treated as UTF-8. This modifier is available from PHP 4.1.0 or greater on Unix and from PHP 4.2.3 on win32. UTF-8 validity of the pattern is checked since PHP 4.3.5.

这篇关于从字符串中删除非字母数字字符(包括 ß、Ê 等)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆