chunk_split_PHP | 使用chunk_split()函数从字符串中拆分固定数量的字符

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-


翻译自: https://www.includehelp.com/php/split-a-fixed-number-of-characters-from-the-string-using-chunk_split-function.aspx

chunk_split

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值