Instance
Splits the string after each character and adds "." After each split. :
<?php$str = "Hello world!"; Echo Chunk_split ($STR, 1, ".");? >
Definition and usage
The Chunk_split () function splits a string into a series of smaller parts.
Note: The function does not change the original string.
Grammar
Chunk_split (String,length,end)
parameters |
description |
string |
required. Specifies the string to be split. |
length |
optional. A number that defines the length of the string block. The default is 76. |
end |
optional. A string that defines what is placed after each string block. The default is \ r \ n. |
Technical details
return value: |
Returns the split string. |
PHP version: |
4 + |
More examples
Example 1
Split the string once every six characters and add "..." after each split:
<?php$str = "Hello world!"; Echo Chunk_split ($STR, 6, "...");? >
supports wide-character segmentation (splits the string into a series of smaller parts)
<?php/** * Split String * @param string $str character to be split * @param int $length specified length * @param string $end The content appended to the segmented block of String */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 Children's Day immediate $ on DFS to $@ #了 '; $str 1 = ' aabbccddeefff '; Echo Mb_chunk_split ($STR, 3, ' ... ', true); S Children's Day ... Immediate $ ... Df...s to ... $@# ... The echo "<br>"; Echo Mb_chunk_split ($str 1, 2, ' ... '); aa...bb...cc ... Dd... Ee...ff ... F