Problems with inconsistent results of a regular expression match in PHP

Source: Internet
Author: User
My custom function is as follows, the function is to match out special characters:

function hasSpecialChar($subject) {    $pattern = "/[`~!@#$%^&*()+=|{}':;',\\[\\].<>\/?~!@#¥%……&*()——+|{}【】‘;:”\"“’。,、?]/";    if (preg_match($pattern, $subject)) {        return true;    }    return false;}

My results:

var_dump(hasSpecialChar('问答社区')); // boolean truevar_dump(hasSpecialChar('Segmentfault')); // boolean falsevar_dump(hasSpecialChar('@Segmentfault')); // boolean true

Can be seen from the first result is that '问答社区' the Chinese character string is matched out of the special characters, but I rubular on the match '问答社区' is not a special character, I ask is not where I write wrong? Or is it a question of the Preg_match function? have been unable to find the reason.

The brothers who pass by help, thank you.

Reply content:

My custom function is as follows, the function is to match out special characters:

function hasSpecialChar($subject) {    $pattern = "/[`~!@#$%^&*()+=|{}':;',\\[\\].<>\/?~!@#¥%……&*()——+|{}【】‘;:”\"“’。,、?]/";    if (preg_match($pattern, $subject)) {        return true;    }    return false;}

My results:

var_dump(hasSpecialChar('问答社区')); // boolean truevar_dump(hasSpecialChar('Segmentfault')); // boolean falsevar_dump(hasSpecialChar('@Segmentfault')); // boolean true

Can be seen from the first result is that '问答社区' the Chinese character string is matched out of the special characters, but I rubular on the match '问答社区' is not a special character, I ask is not where I write wrong? Or is it a question of the Preg_match function? have been unable to find the reason.

The brothers who pass by help, thank you.

But I don't have a special character to match the ' question and answer community ' on rubular.

Rubular is based on Ruby, and Ruby 1.9 has a good support for Chinese in the future.

Excuse me, is there a mistake I wrote?

Need to add u modifiers to turn on UTF-8 support

$pattern = "/[`~!@#$%^&*()+=|{}':;',\\[\\].<>\/?~!@#¥%……&*()——+|{}【】‘;:”\"“’。,、?]/u";

have been unable to find the reason.

PHP is not good enough for Chinese support.

Chinese is saved in PHP in 16, which brings up the problem.

For example, in your example, the special symbols that you match include the comma in Chinese, and you tell PHP that I want to match a special set of symbols, including the Chinese comma, [,] but PHP doesn't know Chinese, and it hears that you want to match a set of data, including, \xef \xbc , \x8c(the comma is converted to hexadecimal), and then you pass it to the "question and answer community", where the word "area" is translated into hexadecimal \xe5 \x8c \xba . As you can see, here's the \x8c match, so the final result is true .

Similarly, can you try to find the word because it matches the hexadecimal match of the special symbol?

PHP Kanji matching can be considered mb_ corresponding functions, such as: Mb_ereg (), although some problems, but in general can be more convenient to solve the problem.
If you use the GBK character set it is recommended that you consider turning into UTF-8 re-processing.

PHP If you need to handle Zhong Wenjing, be sure to use the lowercase letter U
Just change your code to this one.

$pattern = "/[`~!@#$%^&*()+=|{}':;',\\[\\].<>\/?~!@#¥%……&*()——+|{}【】‘;:”\"“’。,、?]/u";

But I do this a lot.

$pattern = "/[`~!@#$%^&*()+=|{}':;',\\[\\].<>\/?~!@#¥%……&*()——+|{}【】‘;:”\"“’。,、?]/isu";
  • 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.