Example code that uses the function substr and ICONV_SUBSTR,MB_SUBSTR
Copy Code code as follows:
<title> Intercept Chinese string _ cloud-Habitat Community _www.jb51.net</title>
<body>
<?php
$STR = ' 123 cloud-dwelling community 456 welcome everyone ';
Echo substr ($str, 0,4);
Echo ' <br> ';
Echo iconv_substr ($str, 0,4, ' utf-8 ');
Echo ' <br> ';
Echo mb_substr ($str, 0,4, ' utf-8 ');
?>
</body>
Description
The above code used two functions Iconv_substr and MB_SUBSTR, can be in the current character string interception, in order to achieve the interception of Chinese characters is not garbled.
How should you choose?
1. The Iconv library may not function correctly on some operating systems, and the GNU Extension Library needs to be installed to ensure its proper operation. The MB_SUBSTR function is better compatible.
2, the Iconv function will first convert the current string to the appropriate encoding and then intercept, and the MB function is directly based on the specified encoding interception (provide secure multi-byte interception), so MB function interception more efficient.
Therefore, the MB_SUBSTR function is the most suitable choice for the interception of Chinese strings.