PHP uses preg_split () to separate special characters (metacharacters, etc.).
This article describes how PHP uses preg_split () to separate special characters (metacharacters, etc. We will share this with you for your reference. The details are as follows:
The special characters mentioned here are special characters used in regular expressions, such as: |. +
If you don't want to talk about anything else, come to an instance:
$pattern="/[,-\\|\\.]/";$subject="aaa,bbb,ccc-ddd-eee-fff|ggg|hhh.iii.jjj.kkk";$spr=preg_split($pattern, $subject);print_r($spr);
Result:
Array ([0] => [1] => [2] => [3] => [4] => [5] => [6] => [7] => [8] => [9] => [10] => [11] => [12] => [13] => [14] => [15] => [16] => [17] => [18] => [19] => [20] => [21] => [22] => [23] => [24] => [25] => [26] => [27] => [28] => [29] => [30] => [31] => [32] => [33] => [34] => [35] => [36] => [37] => [38] => [39] => [40] => [41] => [42] => [43] =>)
Obviously, this is not the result we want. After a while, we can find out why:
Put the special characters in the Regular Expression in front of it and it will be okay, that is
$ Pattern = "/[\\|\..,-]/";
Result:
Array ([0] => aaa [1] => bbb [2] => ccc [3] => ddd [4] => eee [5] => fff [6] => ggg [7] => hhh [8] => iii [9] => jjj [10] => kkk)
Okay, that's the result.
To sum up, when using metacharacters in regular expressions to separate multiple characters in [], put the metacharacters to be escaped in front.
PS: here we will provide two very convenient Regular Expression tools for your reference:
JavaScript Regular Expression online testing tool:
Http://tools.jb51.net/regex/javascript
Regular Expression generation tool:
Http://tools.jb51.net/regex/create_reg