PHP regular expression special characters [: alnum:] [: Alpha:], etc


If “^” appears in “[]”, it means that the expression does not match the characters in “[]”, such as “/ [^ A-Z] /” does not match any lowercase letters! And the regular expression gives several default values of “[]”, as follows:

‘[: alnum:]’ matches any letter
Alphanumeric characters: ‘[:alpha:]’ and ‘[:digit:]’.

‘[: Alpha:]’ matches any letter or number
Alphabetic characters: ‘[:lower:]’ and ‘[:upper:]’.

Blank characters: space and tab.

Control characters. In ASCII, these characters have octal codes 000 through 037, and 177 (‘DEL’). In other character sets, these are the equivalent characters, if any.

‘[: digit:]’ matches any number
Digits: ‘0 1 2 3 4 5 6 7 8 9’.

Graphical characters: ‘[:alnum:]’ and ‘[:punct:]’.

‘[: lower:]’ matches any lowercase letter
Lower-case letters: ‘a b c d e f g h i j k l m n o p q r s t u v w
x y z’.
Printable characters: ‘[:alnum:]’, ‘[:punct:]’, and space.

‘[: punct:]’ matches any punctuation
Punctuation characters: ‘! ” # $ % & ‘ ( ) * + , – . / : ; < = > ? @ [ \ ] ^ _ ‘ { | } ~’.

‘[: Space:]’ matches the space character
Space characters: tab, newline, vertical tab, form feed, carriage
return, and space.

‘[: Upper:]’ matches any uppercase letter
Upper-case letters: ‘A B C D E F G H I J K L M N O P Q R S T U V W
X Y Z’.

‘[: xdigit:]’ matches any hexadecimal number
Hexadecimal digits: ‘0 1 2 3 4 5 6 7 8 9 A B C D E F a b c d e f’.

For example, ‘[[:alnum:]]’ means ‘[0-9A-Za-z]’, except the latter depends upon the ‘C’ locale and the ASCII character encoding, whereas the former is independent of locale and character set. (Note that the brackets in these class names are part of the symbolic names, and must
be included in addition to the brackets delimiting the bracket expression.)

The [$] square brackets contain a matching character range, which is preceded by ^, indicating that it does not match the specified character range.
$ereg represents a variable, that is, $is the symbol of the variable
Ereg stands for all strings that start with a letter and end with a letter or number.


Copy codeThe code is as follows:
preg_replace(“/([[:alnum:]]|[[:space:]]|[[:punct:]])+/U”, ”, $string);