Yesterday, in writing code I encountered a problem of Chinese string interception, originally wanted to use substr (), but this is used in a single-byte character, for multibyte-encoded Chinese characters or other language character, this function may not be used, and will easily intercept garbled to! Now put my work record and summary to share with you: problem generation and Use cases Web site: Weather forecast 15 days query (http://tqybw.net) Problem time: 2013-10-31 solution: The Chinese character according to a Chinese character under the code according to its integrity is divided into a number of groups, And then take the interception length according to the need; 1, the Implementation method function opens the mbstring extension, and then customizes the function:
<?php
Header (' Content-type:text/html:charset=utf-8 ');
function Substr_chinese ($str, $start, $length = null) {return
join ("",
array_slice (
preg_split ("//u", $ STR,-1, Preg_split_no_empty), $start, $length)
);
(PS: ^_^ Good PHP Learning Exchange Group: 276167802, Verification: CSL, thank you!)
//Example
$STR = "Map of China";
Echo Substr_utf8 ($str, 0, 4);
Output: China map
?>
2, the main function description Preg_split: For starters, we are returning to the description array preg_split (string $pattern, string $subject [, int $limit [, int $flags]]) Returns an array that contains substrings that are split along the bounds that match the pattern in subject. If limit is specified, the maximum number of limit substrings is returned, and if limit is-1, it means there are no restrictions that can be used to continue specifying the optional parameter flags. Flags can be any combination of the following tags (in bitwise OR operator combinations): Preg_split_no_empty If this tag is set, Preg_split () returns only the Non-empty component. Preg_split_delim_capture If this tag is set, the bracket expression in the delimiter pattern is also captured and returned. This tag is added to the PHP 4.0.5. Preg_split_offset_capture If you set this tag, if you set this tag, the matching result for each occurrence also returns its subordinate string offset. Note that this changes the value of the returned array so that each cell is also an array, where the first item is a matching string and the second item is its offset in the subject. This tag is available from PHP 4.3.0. Tip If you do not need the functionality of a regular expression, you can choose to use a faster (and simpler) substitution function such as explode () or str_split (). 3, Array_slice Description: Array_slice () function in the array according to the conditions to take out a value, and return.
<?php
$a =array (0=> "Dog",1=> "Cat",2=> "Horse",3=> "Bird");
Print_r (Array_slice ($a, 1,2));
? >
Output: Array ([0] => Cat [1] => horse)
This article tells the PHP Chinese character string interception No garbled method, hoped this article can bring the inspiration to the reader, helps the reader to solve the question, thanks reads this article.