Recently, the boss called a small exercise in data checking, which involves extracting the Chinese text section from a file containing a Chinese text segment and storing it, using PHP for development. The middle involves php regular expression Chinese match the question, the net collects a large, but also very disorderly does not have a notified son, passes through own code modification and the examination, first will extract function writes down.
The first thing to note is that Double-byte character encoding problem, here we may also encounter such as Korean, Japanese and other coding problems, and Chinese understanding is a meaning.
1. GBK (gb2312/gb18030)
Copy Code code as follows:
\x00-\xff GBK Two-byte coding range
\x20-\x7f ASCII
\xa1-\xff Chinese gb2312
\x80-\xff Chinese GBK
2. UTF-8 (Unicode)
Copy Code code as follows:
\u4e00-\u9fa5 (Chinese)
\x3130-\x318f (Korean
\XAC00-\XD7A3 (Korean)
\u0800-\u4e00 (Japanese)
Under notepad++, we can first test the error of our regular writing. The first expression I use [\u4e00-\u9fa5]+ to test, the + number means more than one
The match character. The result is the same as expected, so is it possible to use this regular in a script?
Let's test that we use Preg_match_all ('/[\u4e00-\u9fa5]+/', $subject, $matches) call, and then you see a result: compilation Failed:pcre does Not support \l, \l, \n{name}, \u, or \u at offset 2 .... Isn't it a big head? What is the reason?
Looking at a lot of data, you find that u (PCRE_UTF8), which is the PCRE above, is a Perl library that includes Perl-compatible regular expression libraries. This modifier enables an additional feature that is incompatible with Perl in a PCRE. The pattern string is treated as UTF-8. This modifier is available under Unix from PHP 4.1.0 and is available under Win32 from PHP 4.2.3. PHP Regular expressions also differ in the way hexadecimal data is expressed, in PHP, using \x to represent hexadecimal data. Here we'll optimize the code, and the detection function becomes:
Copy Code code as follows:
Class Storedataadapter extends store{
Private $dsData;
/**
* A data conversion function that invokes Preg_match_all to match the numeric value based on the $pattern, and stores the returned results as an array in the $matches.
* $matches [0] will contain text that matches the entire pattern, $matches [1] will contain text that matches the child pattern in the first captured bracket, and so on
* @see Store::d Ata_convert ()
*/
Public Function Data_convert ($pattern, $subject) {
$matches =array ();
if (Preg_match_all ($pattern, $subject, $matches)) {
return $matches [0];
}else
{
return null;
}
}
}
When the call becomes:
Copy Code code 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 following is the extraction of the contents of the output file after the Chinese:
To meet the expected needs.