PHP divides a string into a series of smaller parts of the function chunk_split ()

Source: Internet
Author: User

Instance

Splits the string after each character and adds "." After each split. :

<?php$str = "Hello world!"; Echo Chunk_split ($STR, 1, ".");? >

Definition and usage

The Chunk_split () function splits a string into a series of smaller parts.

Note: The function does not change the original string.

Grammar

Chunk_split (String,length,end)
parameters description
string required. Specifies the string to be split.
length optional. A number that defines the length of the string block. The default is 76.
end optional. A string that defines what is placed after each string block. The default is \ r \ n.

Technical details

return value: Returns the split string.
PHP version: 4 +

More examples

Example 1

Split the string once every six characters and add "..." after each split:

<?php$str = "Hello world!"; Echo Chunk_split ($STR, 6, "...");? >

supports wide-character segmentation (splits the string into a series of smaller parts)

<?php/** * Split String * @param string $str character to be split * @param int $length specified length * @param string $end The content appended to the segmented block of String */function Mb_chunk_split ($string, $length, $end, $once = False) {$string      = Iconv (' gb2312 ', ' Utf-8//ignore ', $string);      $array = Array ();      $strlen = Mb_strlen ($string);          while ($strlen) {$array [] = Mb_substr ($string, 0, $length, "utf-8"); if ($once) return $array [0].          $end;          $string = Mb_substr ($string, $length, $strlen, "utf-8");      $strlen = Mb_strlen ($string);  } return Implode ($end, $array);  } $str = ' s Children's Day immediate $ on DFS to $@ #了 ';  $str 1 = ' aabbccddeefff '; Echo Mb_chunk_split ($STR, 3, ' ... ', true); S Children's Day ... Immediate $ ... Df...s to ... $@# ...  The echo "<br>"; Echo Mb_chunk_split ($str 1, 2, ' ... '); aa...bb...cc ... Dd... Ee...ff ... F 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.