chunk_split
Given a string and we have to split a fixed number of characters from the string.
给定一个字符串,我们必须从该字符串中拆分固定数量的字符。
When we need a set of small parts of the string (of a fixed number of characters), we can use chunk_split() function. It returns a defined number of characters from the string and can also add a character or string after the string.
当我们需要一组字符串的一小部分(固定数目的字符)时,可以使用chunk_split()函数。 它从字符串返回定义数量的字符,也可以在字符串之后添加一个字符或字符串。
Read more: PHP chunk_split() function
阅读更多: PHP chunk_split()函数
Examples:
例子:
Input:
str = "Hello world!"
//if we want to extract three characters chucks of the string
//no characters at the end (default \r\n will be added)
Function call: chunk_split(str, 3)
Output:
Hel
lo
wor
ld!
Input:
str = "41424344454647484950"
//If we want to extract two characters chunks of the string
//with hyphen (-) after the chunks
Function call: chunk_split(str, 2, "-")
Output:
41-42-43-44-45-46-47-48-49-50
PHP code:
PHP代码:
<?php
$str = "Hello world!";
$split_str = chunk_split($str, 3);
echo ("The extracted characters are...\n");
echo ($split_str);
$str = "41424344454647484950";
$split_str = chunk_split($str, 2, "-");
echo ("The extracted characters are...\n");
echo ($split_str);
?>
Output
输出量
The extracted characters are...
Hel
lo
wor
ld!
The extracted characters are...
41-42-43-44-45-46-47-48-49-50-
chunk_split