Code: {code ...} problem: $ html indicates the webpage data to be extracted. If $ html is UTF-8 encoded, the above Code can be normally executed (that is, Chinese can be extracted), but if it is another encoding, then it cannot be normally executed (Chinese characters cannot be matched). Use iconv to convert the $ html encoding format ,... code:
$ Html = preg_replace_callback ("/(?
[\ X {4e00}-\ x {9fa5}] +)/u ", array (" self "," wyc_chinese "), $ html );... omitted... public function wyc_chinese ($ matches) {return $ matches ['China']. '(Chinese )';}
Problem:
$ Html indicates the webpage data to be extracted.
If $ html is UTF-8 encoded, the above Code can be normally executed (that is, Chinese can be extracted normally), but if it is another encoding, it cannot be executed normally (Chinese characters cannot be matched)
Iconv is used to convert the $ html encoding format, and Chinese characters cannot be extracted normally.
Reply content:
Code:
$ Html = preg_replace_callback ("/(?
[\ X {4e00}-\ x {9fa5}] +)/u ", array (" self "," wyc_chinese "), $ html );... omitted... public function wyc_chinese ($ matches) {return $ matches ['China']. '(Chinese )';}
Problem:
$ Html indicates the webpage data to be extracted.
If $ html is UTF-8 encoded, the above Code can be normally executed (that is, Chinese can be extracted normally), but if it is another encoding, it cannot be executed normally (Chinese characters cannot be matched)
Iconv is used to convert the $ html encoding format, and Chinese characters cannot be extracted normally.
To It is wrong to identify the encoding. Some webpages do not write meta, and modern browsers will also display normally (IE6 is faulty, IE7 and IE8 are not tested ~)
According to the HTTP Response HeaderContent-Type: text/html; charset=UTF-8To determine. If no response is returnedcharset, It is determined based on the content from the line ..
For convenience, it is best to convert htmlUTF-8To perform regular matching.
['Method' => 'get',],]); $ html = file_get_contents ($ remote_url, false, $ context); $ html_encoding = mb_detect_encoding ($ html, ['utf-8', 'cp936', 'ascii ']); // convert to UTF-8 $ target_encoding = 'utf-8 '; $ html = $ target_encoding ===$ html_encoding? $ Html: mb_convert_encoding ($ html, $ target_encoding, $ html_encoding ); // match $ count = preg_match_all ('# [\ x {4e00}-\ x {9fa5}] + # U', $ html, $ matches ); var_dump ($ matches );
The core of your problem is converting web code into UTF-8
You said that the source code is "determined based on the charset field of the meta tag"
I did the same thing, but I succeeded.
You didn't provide detailed code. I don't know where your code went wrong, or simply my character is better than yours.
require_once(__DIR__.'/wp-config.php');$resp = wp_remote_get('http://51nb.com/');$html = $resp['body'];preg_match('@charset=([-a-z0-9_]+)@i',$html,$charset);$html = iconv(strtoupper($charset[1]), "UTF-8", $html);preg_match_all("@\p{Han}+@u",$html,$m); echo '
';print_r($m);exit;
Iconv with the above Code
Iconv without the above Code