PHP | chunk_split()函数

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

null

语法:

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

参数: 此函数接受上述语法中所示的三个参数,如下所述:

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

返回值: 此函数用于返回被分割为较小块的字符串。

例如:

Input : string = "geeksforgeeks"         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
// 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
// PHP program to illustrate the
// chunk_split function
$str = "geeksforgeeks" ;
// Returns a string with a '.'
// placed after every four characters.
echo chunk_split ( $str , 4, "." );
?>


输出:

geek.sfor.geek.s. 

方案3:

PHP

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


输出:

abcd@@

方案4:

PHP

<?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

© 版权声明
THE END
喜欢就支持一下吧
点赞7 分享