chunk_split

(PHP 3 >= 3.0.6, PHP 4, PHP 5)

chunk_split -- 将字符串分割成小块

描述

string chunk_split ( string body [, int chunklen [, string end]] )

使用此函数将字符串分割成小块非常有用。例如将 base64_encode() 的输出转换成符合 RFC 2045 语义的字符串。它会在每 chunklen(默认为 76)个字符后边插入 end(默认为“\r\n”)。此函数会返回新的字符串,而不会修改原有字符串。

例子 1. chunk_split() 示例

<?php
// 使用 RFC 2045 语义格式化 $data
$new_string = chunk_split(base64_encode($data));
?>

参见 str_split()explode()split()wordwrap()RFC 2045


add a note add a note User Contributed Notes
chris AT w3style.co DOT uk
15-Aug-2006 03:32
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.
xamine at gmail dot com
05-Jun-2006 02:06
In reply to "adrian at zhp dot inet dot pl" digit grouping function:
<?php
   $number
= strrev(chunk_split (strrev($number), 3,' '));
  
//If $number is '1234567', result is '1 234 567'.
?>

There is a much more simple way of doing this, by using the built-in number_format() function.

<?php
   $number
= number_format($number,2,"."," ");

  
//This will round $number to 2 decimals, use the dot (".")
   //as decimal point, and the space (" ") as thousand sepparator.

?>
kevin @t hyguard,com
04-Oct-2005 10:08
Not quite completely obvious, but...

you can un_chunk_split() by:

$long_str = str_replace( "\r\n", "", $chunked_str );
harish at thespitbox dot net
30-Jul-2005 08:45
another way to group thousands in a number, which is much simpler, is built into PHP :)

www.php.net/number_format
adrian at zhp dot inet dot pl
09-Jul-2005 05:28
If you need to output number formated with thousand's separator, just use it:

$number = strrev(chunk_split (strrev($number), 3,' '));

If $number is '1234567', result is '1 234 567'.
Kevin
26-Jun-2005 07:10
To phpkid:

This is a much simpler solution.

<?php
function longWordWrap($string) {
  
$string = str_replace("\n", "\n ", $string); // add a space after newline characters, so that 2 words only seperated by \n are not considered as 1 word
  
$words = explode(" ", $string); // now split by space
  
foreach ($words as $word) {
      
$outstring .= chunk_split($word, 12, " ") . " ";
   }
   return
$outstring;
}
?>
Chris
16-Jun-2005 05:18
@phpkid:

You can avoid such long complex code and just use some CSS stuff.

Just add style="table-layout:fixed" in your <td > tag and your problem will be solved.

ciao
phpkid
29-May-2005 01:47
Well I have been having issues with a shoutbox I am coding it would keep expanding the <TD> 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($CurArray<=$Max_Parts)
   {
       $Peice_Size = strlen($Exploded[$CurArray]);
       if($Peice_Size>15)
       {
           $OutString .= chunk_split($Exploded[$CurArray], 12, " ");
           $CurArray++;
       } else {
           $OutString .= " ".$Exploded[$CurArray];
           $CurArray++;
       }
   }
  
   return $OutString;
}
mv@NOSPAM
24-Jan-2004 11:39
the best way to solve the problem with the last string added by chunk_split() is:

<?php
$string
= '1234';
substr(chunk_split($string, 2, ':'), 0, -1);
// will return 12:34
?>
Danilo
11-Dec-2003 06:51
>> 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
sbarnum at pointsystems dot com
21-Apr-2001 10:46
[Editor's note: You can always use wordwrap('FF99FF', 2, ':', 2); to avoid this]

chunk_split will also add the break _after_ the last occurence.  So, attempting to split a color into base components,
chunk_split('FF99FF', 2, ':');
will return FF:99:FF: