PHP如何使用chunk_split()函数?详细示例

2021年3月27日15:16:56 发表评论 626 次浏览

chunk_split()function是PHP中的内置函数。 chunk_split()函数用于将字符串拆分为特定长度的较小块。

语法如下:

string chunk_split($string, $length, $end)

参数:此函数接受上述语法中所示的三个参数, 并在下面进行描述:

  1. $字符串:此参数指定需要分块的字符串。
  2. $ length:此参数指定一个整数, 该整数指定块的长度。那就是分块部分的长度。
  3. $ end:此参数指定行结束顺序。

返回值:此函数返回拆分成较小块的字符串。

例子:

Input : string = "lsbin" 
        length = 4
        end = "."
Output: Geek.sfor.Geek.s. 


Input: string = "Twinkle bajaj" 
       length = 2 
       end = "*" 
Output: Tw*in*kl*e *ba*ja*j*

下面的程序说明了PHP中的chunk_split()函数:

程序1:

<?php
// PHP program to illustrate the  
// chunk_split function 
  
$str = "Twinkle bajaj" ;
echo chunk_split ( $str , 2, "*" );
  
?>

输出如下:

Tw*in*kl*e *ba*ja*j*

程式2:

<?php
// PHP program to illustrate the  
// chunk_split function 
  
$str = "lsbin" ;
  
// Returns a string with a '.'
// placed after every four characters.
echo chunk_split ( $str , 4, "." );
?>

输出如下:

geek.sfor.geek.s.

计划4:

<?php
// PHP program to illustrate the  
// chunk_split function 
  
$str = "abcd" ;
echo chunk_split ( $str , 4, "@@" );
?>

输出如下:

abcd@@

计划5:

<?php
// PHP program to illustrate the  
// chunk_split function 
  
// If specified length is more than
// string length, then added at the
// end.
$str = "abcd" ;
echo chunk_split ( $str , 10, "@@" );
?>

输出如下:

abcd@@

参考:

http://php.net/manual/en/function.chunk-split.php


木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: