PHP: preg_replace_callback matching Chinese Characters

Source: Internet
Author: User
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

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.