• 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 str_split() – Convert a string to an array

by admin

The str_split() function is used to split a string into an array. This function was introduced in PHP5. The function splits the string into an array of characters, each containing length characters; if the length is not specified, it defaults to 1.

Syntax:

str_split(string $string, int $length = 1): array

Parameters

Parameter Description
string The input string.
length Maximum length of the chunk.

Return Values

false is returned if length is less than 1. If the length length exceeds the length of string, the entire string is returned as the first (and only) array element.

Examples

Example 1:

<?php
$text = “How are you”; $split1 = str_split($text); 
$split2 = str_split($text, 3); 
print_r($split1); 
print_r($split2);
?>

The output of the above program is as follows:

Array (
[0] => H 
[1] => o 
[2] => w 
[3] => 
[4] => a 
[5] => r 
[6] => e 
[7] => 
[8] => y 
[9] => o 
[10] => u )
Array
(
[0] => How 
[1] => ar 
[2] => e y 
[3] => ou )

Filed Under: PHP

Some more articles you might also be interested in …

  1. PHP function strlen() – Get string length
  2. PHP function stripcslashes() – Un-quote string quoted with addcslashes()
  3. PHP function chunk_split() – Split a string into smaller chunks
  4. How to test a PHP script
  5. PHP function str_ireplace() – Case-insensitive version of str_replace()
  6. PHP connection_status function – Returns connection status bitfield
  7. PHP addslashes function – Quote string with slashes
  8. PHP function str_repeat() – Repeat a string
  9. PHP decbin function – Decimal to binary
  10. PHP bindec function – Convert a number between arbitrary bases

You May Also Like

Primary Sidebar

Recent Posts

  • JavaFX ComboBox: Set a value to the combo box
  • Nginx load balancing
  • nginx 504 gateway time-out
  • Images preview with ngx_http_image_filter_module

© 2022 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright