php7 str split,chunk_split()

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行尾序列符号。

返回值

返回分割后的字符。

范例

Example #1chunk_split()例子<?php

// 使用 RFC 2045 语义格式化 $data

$new_string = chunk_split(base64_encode($data));

?>

参见split() 用正则表达式将字符串分割到数组中

» RFC 2045An alternative for unicode strings;

function chunk_split_unicode($str, $l = 76, $e = "\r\n") {

$tmp = array_chunk(

preg_split("//u", $str, -1, PREG_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($str, 4) ."\n";

echo chunk_split_unicode($str, 4);

?>

Yar�

�m k

ilo

çay

, ya

rım

kil

o ş

eker

Yarı

m ki

lo ç

ay,

yarı

m ki

lo ş

ekerAs an alternative for qeremy [atta] gmail [dotta] com

There is much shorter way for binarysafe chunking of multibyte string:

function word_chunk($str, $len = 76, $end = "\n") {

$pattern = '~.{1,' . $len . '}~u'; // like "~.{1,76}~u"

$str = preg_replace($pattern, '$0' . $end, $str);

return rtrim($str, $end);

}

$str = 'русский';

echo chunk_split($str, 3) ."\n";

echo word_chunk($str, 3) . "\n";

?>

р�

�с

с�

�и

й

рус

ски

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

$string = '1234';

substr(chunk_split($string, 2, ':'), 0, -1);

// will return 12:34

?>If you are using UTF-8 charset you will face a problem with Arabic language

to solve this problem i used this function

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;

}

?>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 languageanother way to group thousands in a number, which is much simpler, is built into PHP :)

www.php.net/number_formatImportant 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).

function HtmlEntitySafeSplit($html,$size,$delim)

{

$pos=0;

for($i=0;$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;

}

?>>> 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:FFI'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.

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;

}

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

$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.

$number = number_format($number,2,"."," ");

//This will round $number to 2 decimals, use the dot (".")

//as decimal point, and the space (" ") as thousand sepparator.

?>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 SvaneWell 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($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;

}@Royce

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

function HtmlEntitySafeSplit($html,$size,$delim)

{

$pos=0;

for($i=0;$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;

}

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

function mbStringToArray ($str) {

if (empty($str)) return false;

$len = mb_strlen($str);

$array = array();

for ($i = 0; $i < $len; $i++) {

$array[] = mb_substr($str, $i, 1);

}

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;

}

?>To phpkid:

This is a much simpler solution.

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;

}

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值