The function substr and iconv_substr,mb_substr are used.
php $str = ' 123 Script House 456 Welcome to ' 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 uses two functions Iconv_substr and MB_SUBSTR, can be in the current character under the string interception, in order to achieve the truncation of Chinese characters are not garbled.
How should we choose?
1. The Iconv library may not run 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 a secure multi-byte interception), so the MB function is more efficient interception.
Therefore, the MB_SUBSTR function is the most appropriate choice for intercepting Chinese strings.
The function substr and iconv_substr,mb_substr are used.