This article mainly introduces how PHP uses preg_split () to separate special characters (metacharacters, etc.), and analyzes the operation skills and precautions of php regular expression segmentation based on specific examples, for more information about how to use preg_split () to separate special characters (metacharacters, etc.) in PHP, see the following example. 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.
For more information about how to use preg_split () to separate special characters (metacharacters, etc.) in PHP, refer to PHP!