Please explain this section of php code about the preg_replace_callback () function.

Source: Internet
Author: User
{Code...} I searched for the relevant information and understood it, but I still did not understand the essence. 1) The first parameter of the preg_replace_callback () function should be a regular expression used to match relevant characters. However, I do not quite understand the two '#' and the last I, for the sake...
$str =  preg_replace_callback(            "#\\\u([0-9a-f]{4})#i",             function($matchs)             {                 return  iconv('UCS-2BE', 'UTF-8',  pack('H4',  $matchs[1]));             },             $str); 

I searched for the relevant information and got a rough idea, but I still don't understand the essence.
1) The first parameter of the preg_replace_callback () function should be a regular expression used to match relevant characters. However, I don't quite understand the two '#' and the last I. Why?
2) The second parameter is the parameter $ matchs specified by the system in the closure? Or do I just give a parameter name a row? For example, can a function ($ a) be a closure or a function rewrite?
3) for this $ matchs, after my test output, it is an array. matchs [0] is u8fd8, and matchs [1] is 8fd8. How is this done? Using the regular expression above, I can only match the results of matchs [0], remove the work of u, and use matchs [1] to retain the results after removing u? Is it the I after?

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

4) 'iconv ('ucs-2be ', 'utf-8', pack ('h4', $ matchs [1]); after output, iconv () the return value of the function is also how the iconv () function is assembled internally? What is the role of H4 in the internal pack function? How is it added before 8FD8.

For more questions about new users, please kindly advise. Grateful!

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

I searched for the relevant information and got a rough idea, but I still don't understand the essence.
1) The first parameter of the preg_replace_callback () function should be a regular expression used to match relevant characters. However, I don't quite understand the two '#' and the last I. Why?
2) The second parameter is the parameter $ matchs specified by the system in the closure? Or do I just give a parameter name a row? For example, can a function ($ a) be a closure or a function rewrite?
3) for this $ matchs, after my test output, it is an array. matchs [0] is u8fd8, and matchs [1] is 8fd8. How is this done? Using the regular expression above, I can only match the results of matchs [0], remove the work of u, and use matchs [1] to retain the results after removing u? Is it the I after?

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

4) 'iconv ('ucs-2be ', 'utf-8', pack ('h4', $ matchs [1]); after output, iconv () the return value of the function is also how the iconv () function is assembled internally? What is the role of H4 in the internal pack function? How is it added before 8FD8.

For more questions about new users, please kindly advise. Grateful!

1. regular wildcard characters indicate case-insensitive
2 $ match variable name. Generally
Preg_replace_callback ("/expression/wildcard", array ($ this, "a"), $ string );
Function a ($ m ){

}
3. you cannot answer a matched string.

  1. # It can be replaced with any special symbol, as long as it is not common, you can use "~ ","/"," # "Are the same. I indicates case insensitive.

  2. Write at will, not necessarily $ match

  3. Iconv is the encoding conversion function.

The function of the above code is only used to convert the character string of the UCS-2BE character format into Chinese characters.

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

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.