• 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 strcasecmp() – Binary safe case-insensitive string comparison
  2. PHP function abs – Absolute value
  3. PHP function str_repeat() – Repeat a string
  4. PHP Function strncasecmp() – Binary safe case-insensitive string comparison of the first n characters
  5. PHP function chr() – Generate a single-byte string from a number
  6. PHP base_convert function – Convert a number between arbitrary bases
  7. PHP addslashes function – Quote string with slashes
  8. PHP Function str_word_count() – Return information about words used in a string
  9. “Access denied for user ‘username’@’hostname’ (using password: YES)” – Error while connecting MySQL with PHP
  10. PHP function str_ireplace() – Case-insensitive version of str_replace()

You May Also Like

Primary Sidebar

Recent Posts

  • Chezmoi: A multi-machine dotfile manager, written in Go
  • cheat: Create and view interactive cheat sheets on the command-line
  • chars: Display names and codes for various ASCII and Unicode characters and code points
  • chafa: Image printing in the terminal

© 2023 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright