Using Regular Expressions in PHP to extract Chinese notes
This article mainly introduces the use of regular expressions in PHP to extract Chinese Implementation notes. This article also describes Regular Expressions in Korean and Japanese, and provides implementation code and examples, for more information, see
Recently, my boss called a data re-check exercise, which involves extracting and storing Chinese fields from a file containing Chinese fields and using php for development. In the middle, php regular expressions are involved in Chinese matching. I have collected a large number of documents on the Internet, but there is no such thing as a letter of admission. After modifying and verifying my code, I will write down the extract function first.
First of all, we should note that the problem of dual-byte character encoding may also occur in the future, such as Korean and Japanese encoding.
1. GBK (GB2312/GB18030)
The Code is as follows:
\ X00-\ xff GBK dubyte encoding range
\ X20-\ x7f ASCII
\ Xa1-\ xff Chinese gb2312
\ X80-\ xff Chinese gbk
2. UTF-8 (Unicode)
The Code is as follows:
\ U4e00-\ u9fa5 (Chinese)
\ X3130-\ x318F (Korean
\ XAC00-\ xD7A3 (Korean)
\ U0800-\ u4e00 (Japanese)
Below Notepad ++, We can first test whether the regular expression is incorrect. I used [\ u4e00-\ u9fa5] + to test the first expression. The + number indicates more than one expression.
Match character. The result is the same as expected. Can this regular expression be used in the script?
Let's test it. We use preg_match_all ('/[\ u4e00-\ u9fa5] +/', $ subject, $ matches) to call it. Then you can see this result: compilation failed: PCRE does not support \ L, \ l, \ N {name}, \ U, or \ u at offset 2 .... Is it big ?? What is the reason?
After reading a lot of information, we found that u (PCRE_UTF8) is the above PCRE, which is a Perl library, including a perl-Compatible Regular Expression Library. This modifier enables additional features that are not compatible with Perl in a pcre. The pattern string is treated as a UTF-8. This modifier is available in Unix from PHP 4.1.0 and win32 from PHP 4.2.3. Php regular expressions have different expressions for hexadecimal data. In php, \ x is used to represent hexadecimal data. Below we will optimize the code and change the detection function:
The Code is as follows:
Class storeDataAdapter extends Store {
Private $ dsData;
/**
* Data Conversion Function. Call preg_match_all to match the value based on the $ pattern regular and store the returned results in an array in $ matches,
* $ Matches [0] will contain the text that matches the entire pattern, $ matches [1] will contain the text that matches the child pattern in the first captured bracket, and so on
* @ See Store: data_convert ()
*/
Public function data_convert ($ pattern, $ subject ){
$ Matches = array ();
If (preg_match_all ($ pattern, $ subject, $ matches )){
Return $ matches [0];
} Else
{
Return null;
}
}
}
Changed:
The Code is as follows:
$ Store = new storeDataAdapter ($ txtContent );
$ Match = array ();
$ DsName = $ store-> data_convert ('/[\ x7f-\ xff] +/', $ txtContent );
Foreach ($ dsName as $ val ){
Echo $ val. "<br> ";
}
The input file is:
The output file content after extracting Chinese characters:
.