百科狗-知识改变命运!
--

chunk_split() - 将字符串分割成小块 - php 字符串函数

百变鹏仔1年前 (2023-11-21)阅读数 21#技术干货
文章标签字符串

chunk_split()

(PHP 4, PHP 5, PHP 7)

将字符串分割成小块

说明

chunk_split(string $body[,int $chunklen= 76[,string $end= "rn"]]) : string

使用此函数将字符串分割成小块非常有用。例如将base64_encode()的输出转换成符合 RFC 2045 语义的字符串。它会在每$chunklen个字符后边插入$end。

参数

$body

要分割的字符。

$chunklen

分割的尺寸。

$end

行尾序列符号。

返回值

chunk_split() - 将字符串分割成小块 - php 字符串函数

返回分割后的字符。

范例

Example #1chunk_split()例子

参见

  • str_split()将字符串转换为数组
  • explode()使用一个字符串分割另一个字符串
  • split() 用正则表达式将字符串分割到数组中
  • wordwrap()打断字符串为指定数量的字串
  • » RFC 2045
An alternative for unicode strings;

Yar�
�m k
ilo 
çay
, ya
rım
 kil
o ş
eker
Yarı
m ki
lo ç
ay, 
yarı
m ki
lo ş
eker
As an alternative for qeremy [atta] gmail [dotta] com
There is much shorter way for binarysafe chunking of multibyte string:

р�
�с
с�
�и
й
рус
ски
й
I'm not sure what versions this also occurs in but the output of chunk_split() in PHP 5.0.4 does not match the output in other versions of PHP.
In all versions of PHP I have used, apart from 5.0.4 chunk_split() adds the separator (\r\n) to the end of the string. But in PHP 5.0.4 this does not happen. This had a fairly serious impact on a library I maintain so it may also affect others who are not aware of this.
The description of the function is slightly inaccurate. A trailing $end is also added.
the best way to solve the problem with the last string added by chunk_split() is:
If you are using UTF-8 charset you will face a problem with Arabic language
to solve this problem i used this function
Not quite completely obvious, but... 
you can un_chunk_split() by:
$long_str = str_replace( "\r\n", "", $chunked_str );
"version" of chunk_split for cyrillic characters in UTF-8
public function UTFChunk($Text,$Len = 10,$End = "\r\n")
{
  if(mb_detect_encoding($Text) == "UTF-8")
  {
    return mb_convert_encoding(
        chunk_split(
          mb_convert_encoding($Text, "KOI8-R","UTF-8"), $Len,$End
        ),
        "UTF-8", "KOI8-R"
      );
  } else
  {
    return chunk_split($Text,$Len,$End);
  }
}
this is example for russian language
another way to group thousands in a number, which is much simpler, is built into PHP :)
www.php.net/number_format
Important note is the maximum line length and the recommended one. The standard says:
"Lines in a message MUST be a maximum of 998 characters excluding the CRLF, but it is RECOMMENDED that lines be limited to 78 characters excluding the CRLF. "
See PHP manual for chunk_split() Which is set to 76 characters long chunk and "\r\n" at the end of line by default.
Here's a version of Chunk Split I wrote that will not split html entities. Useful if you need to inject something in html (in my case,  tags to allow for long text wrapping).
>> chunk_split will also add the break _after_ the last occurence.
this should be not the problem 
substr(chunk_split('FF99FF', 2, ':'),0,8);
will return FF:99:FF
I've found this quite useful for simulating various kinds of shuffles with cards. It is humorous but can imitate multiple deck cuts and other (imperfectly) random events.
When using ssmtp for simple command line mailing:
$mail_to = "destination@emailbox.com";
$msg = "this would be an actual base64_encoded gzip msg";
$date = date(r);
$mail = "X-FROM: root@sender.org \n";
$mail .= "X-TO: ".$mail_to. " \n";
$mail .= "To: ".$mail_to. " \n";
$mail .= "Date: $date \n";
$mail .= "From: root@sender.org \n";
$mail .= "Subject: lifecheck \n";
$mail .= $msg." \n";
exec("echo '$mail'  |  /usr/sbin/ssmtp ".$mail_to);
be sure to invoke chunk_split() on your message body - ssmtp becomes unhappy with long lines and will subsequently trash your message.
In reply to "adrian at zhp dot inet dot pl" digit grouping function:

There is a much more simple way of doing this, by using the built-in number_format() function. 
This function is very simple and many other functions make this on PHP 5 and even some ones in 4 the good think about this one is that work on php 3.0.6 and 4 
function split_hjms_chars($xstr, $xlenint, $xlaststr)
{
  $texttoshow = chunk_split($xstr,$xlenint,"\r\n"); 
  $texttoshow = split("\r\n",$texttoshow);
  $texttoshow = $texttoshow[0].$xlaststr;
  return $texttoshow;
}
// For use 
echo split_hjms_chars("This is your text",6,"...");
// Will return
This i...
It is useful to cut long text on preview lists and if the server it's old.
Hope it helps some one. Hans Svane
Well I have been having issues with a shoutbox I am coding it would keep expanding the  if there were large words in it but I fixed it with this:
function PadString($String){
  $Exploded = explode(" ", $String);
  $Max_Parts = count($Exploded);
  
  $CurArray = 0;
  $OutString = '';
  while($CurArray15)
    {
      $OutString .= chunk_split($Exploded[$CurArray], 12, " ");
      $CurArray++;
    } else {
      $OutString .= " ".$Exploded[$CurArray];
      $CurArray++;
    }
  }
  
  return $OutString;
}
@Royce
I think this is better, since you can still use the ampersand in your text:
chunk_split() is not multibyte safe. If you ever run into needing the function that is multibyte safe, here you go:
To phpkid:
This is a much simpler solution.

鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)