字符串函数
在线手册:中文  英文

chunk_split

(PHP 4, PHP 5)

chunk_split将字符串分割成小块

说明

string chunk_split ( string $body [, int $chunklen = 76 [, string $end = "\r\n" ]] )

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

参数

body

要分割的字符。

chunklen

分割的尺寸。

end

行尾序列符号。

返回值

返回分割后的字符。

范例

Example #1 chunk_split() 例子

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

参见


字符串函数
在线手册:中文  英文

用户评论:

zeusgerde at arcor dot de (2012-07-10 07:14:49)

Three-liner for multibyte version of chunk_split()

<?php

function mb_chunk_split($body$chunklen 76$end "\r\n") {
    return 
join($endpreg_split('/(.{'$chunklen .'})/us'$body0PREG_SPLIT_NO_EMPTY PREG_SPLIT_DELIM_CAPTURE));
}

?>

preg_split() splits the string into $chunklen pieces
join() glues everything together to one string

PCRE_UTF8 makes it a multibyte version
PCRE_DOTALL makes sure that line breaks are also captured
PREG_SPLIT_NO_EMPTY removes the empty lines that were captured by the regular expression
PREG_SPLIT_DELIM_CAPTURE re-adds the non-empty lines that were captured by the regular expression (first sub-pattern)

qeremy [atta] gmail [dotta] com (2012-02-28 15:35:26)

An alternative for unicode strings;

<?php
function chunk_split_unicode($str$l 76$e "\r\n") {
    
$tmp array_chunk(
        
preg_split("//u"$str, -1PREG_SPLIT_NO_EMPTY), $l);
    
$str "";
    foreach (
$tmp as $t) {
        
$str .= join(""$t) . $e;
    }
    return 
$str;
}

$str "Yar?m kilo ?ay, yar?m kilo ?eker";
echo 
chunk_split($str4) ."\n";
echo 
chunk_split_unicode($str4);
?>

Yar?
?m k
ilo 
?ay
, ya
r?m
 kil
o ?
eker

Yar?
m ki
lo ?
ay, 
yar?
m ki
lo ?
eker

belal dot nabeh at gmail dot com (2010-08-10 08:02:45)

If you are using UTF-8 charset you will face a problem with Arabic language
to solve this problem i used this function

<?php
function chunk_split_($text,$length,$string_end)
{
    
$text iconv("UTF-8","windows-1256",$text);
    
$text str_split($text);
    foreach(
$text as $val)
    {
        if(
$a !== $val)
        {
            
$a $val;
            
$x 0;
        }else{
            
$a $val;
            
$x++;
        }
        if(
$x $length)
        {
            
$new_text .= $val.$string_end;
            
$x 0;
        }else
        {
             
$new_text .= $val;
        }
        
    }
    
$new_text iconv("windows-1256","UTF-8",$new_text);
    return 
$new_text;
}
?>

Peter from dezzignz.com (2010-04-05 06:07:06)

Oops. Here is a correction for the post below.

<?php

function mbStringToArray ($str) {
    if (empty(
$str)) return false;
    
$len mb_strlen($str);
    
$array = array();
    for (
$i 0$i $len$i++) {
        
$array[] = mb_substr($str$i1);
    }
    return 
$array;
}

function 
mb_chunk_split($str$len$glue) {
    if (empty(
$str)) return false;
    
$array mbStringToArray ($str);
    
$n = -1;
    
$new '';
    foreach (
$array as $char) {
        
$n++;
        if (
$n $len$new .= $char;
        elseif (
$n == $len) {
            
$new .= $glue $char;
            
$n 0;
        }
    }
    return 
$new;
}

// DEMO:
$str '一二一二一二一二一二';
echo 
mb_chunk_split($str2'*<br />');

// produces this output:
一二*
一二*
一二*
一二*
一二

?>

Peter from dezzignz.com (2010-04-05 04:30:57)

chunk_split() is not multibyte safe. If you ever run into needing the function that is multibyte safe, here you go:

<?php

function mbStringToArray ($str) {
    if (empty(
$str)) return false;
    
$len mb_strlen($str);
    
$array = array();
    for (
$i 0$i $len$i++) {
        
$array[] = mb_substr($str$i1);
    }
    return 
$array;
}

function 
mb_chunk_split($str$len$glue) {
    if (empty(
$str)) return false;
    
$array mbStringToArray ($str);
    
$n 0;
    
$new '';
    foreach (
$array as $char) {
        if (
$n $len$new .= $char;
        elseif (
$n == $len) {
            
$new .= $glue $char;
            
$n 0;
        }
        
$n++;
    }
    return 
$new;
}

?>

dampee at earthlink dot net (2009-11-20 09:10:11)

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.

<?php
function truffle_shuffle($body$chunklen 76$end "\r\n")
{
     
$chunk chunk_split($body$chunklen"-=blender=-");
     
$truffle explode("-=blender=-",$chunk);
     
$shuffle shuffle($truffle);
     
$huknc implode($end,$shuffle);
     return 
$huknc;
}
?>

tim at weird spots in my crotch dot com (2008-05-08 02:29:38)

@Royce

I think this is better, since you can still use the ampersand in your text:

<?php
function HtmlEntitySafeSplit($html,$size,$delim)
{
  
$pos=0;
  for(
$i=0;$i<strlen($html);$i++)
  {
    if(
$pos >= $size && !$unsafe)
    {
      
$out.=$delim;
      
$unsafe=0;
      
$pos=0;
    }
    
$c=substr($html,$i,1);
    if(
$c == "&")
      
$unsafe=1;
    elseif(
$c == ";")
      
$unsafe=0;
    elseif(
$c == " ")
      
$unsafe=0;
    
$out.=$c;
    
$pos++;
  }
  return 
$out;
}
?>

neos at blizzz dot ru (2008-05-03 11:38:46)

"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

Royce (2008-04-16 00:31:19)

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, <wbr/> tags to allow for long text wrapping).

<?php
function HtmlEntitySafeSplit($html,$size,$delim)
{
  
$pos=0;
  for(
$i=0;$i<strlen($html);$i++)
  {
    if(
$pos >= $size && !$unsafe)
    {
      
$out.=$delim;
      
$unsafe=0;
      
$pos=0;
    }
    
$c=substr($html,$i,1);
    if(
$c == "&")
      
$unsafe=1;
    elseif(
$c == ";")
      
$unsafe=0;
    
$out.=$c;
    
$pos++;
  }
  return 
$out;
}
?>

lehongviet at gmail dot com (2007-06-29 03:23:51)

This function works well to cut long para for preview without cutting word. Good for Unicode such as &#7789;
function split_hjms_chars($xstr, $xlenint, $xlaststr)
{
$xlenint = strpos($xstr," ",$xlenint);
return substr($xstr,0,$xlenint).$xlaststr;
}

mark [at] d0gz [dot] net (2007-04-10 22:12:28)

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.

(2007-03-06 02:45:02)

in response to "hansvane at yahoo dot com dot ar"

you can do that ALOT easier:

<?php
function split_hjms_chars($xstr$xlenint$xlaststr)
{
   return 
substr($xstr,0,$xlenint).$xlaststr;
}
?>

hansvane at yahoo dot com dot ar (2007-01-21 22:06:24)

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

chris AT w3style.co DOT uk (2006-08-14 12:32:05)

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 (2006-06-04 11:06:00)

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 (2005-10-03 19:08:17)

Not quite completely obvious, but...
you can un_chunk_split() by:
$long_str = str_replace( "\r\n", "", $chunked_str );

harish at thespitbox dot net (2005-07-30 05:45:56)

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 (2005-07-08 14:28:37)

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 (2005-06-25 16:10:34)

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($word12" ") . " ";
    }
    return 
$outstring;
}
?>

Chris (2005-06-15 14:18:48)

@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 (2005-05-28 10:47:06)

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 (2004-01-24 07:39:25)

the best way to solve the problem with the last string added by chunk_split() is:

<?php
$string 
'1234';
substr(chunk_split($string2':'), 0, -1);
// will return 12:34
?>

Danilo (2003-12-10 14:51: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 (2001-04-20 19:46:52)

[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:

易百教程