PHP uses preg_split () to separate special characters (metacharacters, etc.).

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.