PHP uses regular expressions to match Chinese characters. some of them cannot be matched.

Source: Internet
Author: User
PHP uses regular expressions to match Chinese characters. some of the solutions that cannot be matched are required during development today, that is, matching the material text in a billing formula. for example: [goat fat jade price] * [goat fat jade weight] + [platinum price] * [platinum weight] + [Hao Stone price] * [Hao stone weight] + [diamond price] * 1.5 * [diamond weight] + [hard gold price] * 1.67 * [hard gold weight], to match the [*** _ price], it is the string ending with _ Price in brackets (representing the name of the material). the language I use is PHP, the code is as follows:

$ Pattern = "// \ [([^ _ Price] +) _ price \]/"; $ res = preg_match_all ($ pattern, $ s, $ matches ); var_dump ($ matches );

, The first three can match, but the "diamond" cannot match, which is very strange. I tested it later and it has nothing to do with the matching quantity, because if you change "diamond" to "Platinum", it can be matched. if you change it to "stone", it can also be matched. Judging from this phenomenon, it may be a problem with character sets. it may be that the default character set does not contain the word "drill", so it cannot be matched, after searching for articles by many netizens about using regular expressions to match Chinese characters in PHP, we found that the most comprehensive matching method was in hexadecimal format. after the change, it was successful, the final code is as follows:
$ S = '[goat fat jade price] * [goat fat jade weight] + [platinum price] * [platinum weight] + [Hao Stone price] * [Hao stone weight] + [diamond price] * 1.5 * [diamond weight] + [price of hard gold] * 1.67 * [weight of hard gold] ';

$ Pattern = "// \ [([\ x {4e00}-\ x {9fa5}] +) _ price \]/u "; $ res = preg_match_all ($ pattern, $ s, $ matches); var_dump ($ matches );

In this way, you can extract all the goat fat jade, platinum, Hao Stone, diamond, and hard gold.

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.