• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • VCS
  • Interview Questions
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

PHP function chunk_split() – Split a string into smaller chunks

by admin

The chunk_split() function divides a string into a sequence of small fragments. chunk_split() adds the character(s) specified in chunk_ending every chunk_length characters. This function is useful for breaking certain types of data into separate lines of a specified length. It’s often used to make base64 encoded data conform to RFC 2045. This function was introduced in PHP3.

Syntax:

string chunk_split(string string, [int chunk_length], [string chunk_ending])

Parameters

Parameter Description
string String to split into chunks
chunk_length Length of the chunks (default 76)
chunk_ending Character(s) to place at the end of each chunk (default \r\n)

Return Values

String with chunk_ending placed every chunk_length

Examples

Example 1:

<?php
// formatting $info by using the RFC 2045 semantics
$new_strng = chunk_split(base64_encode($info)); ?>

Example 2:

$data = "...some long data...";
$converted = chunk_split(base64_encode($data));
Note: This function should not be used for breaking long lines of text into shorter lines for display purposes—use wordwrap() instead.

Filed Under: PHP

Some more articles you might also be interested in …

  1. PHP base_convert function – Convert a number between arbitrary bases
  2. PHP function chop() – Alias of rtrim()
  3. htaccess Cheatsheet
  4. PHP function stripcslashes() – Un-quote string quoted with addcslashes()
  5. PHP function convert_cyr_string() – Convert from one Cyrillic character set to another
  6. PHP atan function – Arc tangent
  7. How to test a PHP script
  8. PHP function bin2hex – Convert binary data into hexadecimal representation
  9. PHP decbin function – Decimal to binary
  10. PHP ceil function – Round fractions up

You May Also Like

Primary Sidebar

Recent Posts

  • fprintd-delete Command Examples in Linux
  • fprintd-delete: command not found
  • foreman: command not found
  • foreman Command Examples in Linux

© 2023 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright