Ask the great God to explain this PHP code, about the Preg_replace_callback () function

Source: Internet
Author: User
$str =  preg_replace_callback(            "#\\\u([0-9a-f]{4})#i",             function($matchs)             {                 return  iconv('UCS-2BE', 'UTF-8',  pack('H4',  $matchs[1]));             },             

I searched the relevant information, understand a probable, but the essence of the part still do not understand.
1) The first parameter of the Preg_replace_callback () function should be a regular expression that matches the relevant character. But, among the two ' # ' and the last I I don't quite understand, for what?
2) The second parameter is a closure of the parameter $matchs is the system specified? Or just give me the name of a parameter? such as function ($a), and this place can only be a closure, can be rewritten with functions?
3) about this $matchs, after I test the output, it is a number of groups. Matchs[0] is u8fd8,matchs[1] is 8fd8. How did this happen? Using the previous regular expression I can only match to the results of matchs[0], minus u work and re-use matchs[1] to retain the result of removing u who did it? Is it the ' # ' after I?

Array(    [0] => \u8fd8    [1] => 8fd8)

4) ' Iconv (' ucs-2be ', ' UTF-8 ', pack (' H4 ', $matchs [1])); After output, the return value of the Iconv () function is also how the Iconv () function is assembled inside? What is the role of H4 in the internal pack function? How to add to the front of the 8fd8.

Novice problem more Please the great gods, generous enlighten. Appreciate!

Reply content:

$str =  preg_replace_callback(            "#\\\u([0-9a-f]{4})#i",             function($matchs)             {                 return  iconv('UCS-2BE', 'UTF-8',  pack('H4',  $matchs[1]));             },             

I searched the relevant information, understand a probable, but the essence of the part still do not understand.
1) The first parameter of the Preg_replace_callback () function should be a regular expression that matches the relevant character. But, among the two ' # ' and the last I I don't quite understand, for what?
2) The second parameter is a closure of the parameter $matchs is the system specified? Or just give me the name of a parameter? such as function ($a), and this place can only be a closure, can be rewritten with functions?
3) about this $matchs, after I test the output, it is a number of groups. Matchs[0] is u8fd8,matchs[1] is 8fd8. How did this happen? Using the previous regular expression I can only match to the results of matchs[0], minus u work and re-use matchs[1] to retain the result of removing u who did it? Is it the ' # ' after I?

Array(    [0] => \u8fd8    [1] => 8fd8)

4) ' Iconv (' ucs-2be ', ' UTF-8 ', pack (' H4 ', $matchs [1])); After output, the return value of the Iconv () function is also how the Iconv () function is assembled inside? What is the role of H4 in the internal pack function? How to add to the front of the 8fd8.

Novice problem more Please the great gods, generous enlighten. Appreciate!

1 I regular wildcard meaning is case insensitive
2 $match variable name can be written casually. This is generally used
Preg_replace_callback ("/Expression/wildcard character", Array ($this, "a"), $string);
function A ($m) {

}
3 You didn't give the matching string an answer.

    1. # can be replaced with any special symbol, as long as the uncommon on the line, you use "~", "/", "#" are the same, I means case-insensitive

    2. Write casually, not necessarily $match

    3. Iconv is a coded conversion function

The function of the above code is only used to convert strings of ucs-2be character format into Chinese characters.

Http://bbs.csdn.net/topics/39 ...

  • Related Article

    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.