Example
Split the string once after each characterString, and add "." after each split:
<?php $str = "Hello world!"; echo chunk_split($str,1,"."); ?>
Definition and usage
chunk_split() function splits a string into a series of smaller parts.
Note: This function does not change the original string.
Syntax
chunk_split(string,length,end)
Parameters | Description |
string | Required. Specifies the string to be split. |
length | Optional. A number defining the length of the string block. Default is 76. |
end | Optional. A string defining what is placed after each string block. Default is \r\n. |
Technical details
Return value: | Returns the split string. |
PHP version: | 4+ |
<?php $str = "Hello world!"; echo chunk_split($str,6,"..."); ?>
Example: Support wide character splitting, (Split the string into a series of smaller parts)
<?php /** * 分割字符串 * @param String $str 要分割的字符串 * @param int $length 指定的长度 * @param String $end 在分割后的字符串块追加的内容 */ function mb_chunk_split($string, $length, $end, $once = false){ $string = iconv('gb2312', 'utf-8//ignore', $string); $array = array(); $strlen = mb_strlen($string); while($strlen){ $array[] = mb_substr($string, 0, $length, "utf-8"); if($once) return $array[0] . $end; $string = mb_substr($string, $length, $strlen, "utf-8"); $strlen = mb_strlen($string); } return implode($end, $array); } $str = 's六一马上$就dfs要到$@#了'; $str1 = 'aabbccddeefff'; echo mb_chunk_split($str, 3, '...', true); //s六一...马上$...就df...s要到...$@#...了 echo "<br>"; echo mb_chunk_split($str1, 2, '...'); //aa...bb...cc...dd...ee...ff...f
The above is the detailed content of PHP splits a string into a series of smaller parts using the chunk_split() function. For more information, please follow other related articles on the PHP Chinese website!