15 1 0 4000 1 https://codeblock.co.za 300 true 0
theme-sticky-logo-alt

str_split()

0 Comments

Programming Language

PHP [Versions: 5, 7, 8]

Syntax

Typical Usage

Converts a string to an array by breaking up the string into chunks specified by the optional length parameter.

Accepted Parameters

  • string (string) (Required): The string you wish to split.
  • length (integer) (Optional) (Default = 1): If specified, the split string will be be the length of length.

Return Value(s)

Returns an array with each key containing a single character or a number of characters specified in the length parameter.

Notes

If  the length is less than 1, str_split will return false. If the length exceeds the length of string, the entire string is returned as the only element in the array.

Examples of str_split()

Example 1

<?php
$string = "Hulk Smash";

$new_array = str_split($string);

print_r($new_array);

Since length is not included, the default of one character per array key. The above will output:

Array
 (
     [0] => H
     [1] => u
     [2] => l
     [3] => k
     [4] => 
     [5] => S
     [6] => m
     [7] => a
     [8] => s
     [9] => h
 )

Example 2

<?php
 $string = "Hulk Smash";

 $new_array = str_split($string);

 print_r($new_array, 2);

Adding a length value will break the string up into chunks before storing the chunks into an array. So the result will be:

Array
  (
      [0] => Hu
      [1] => lk
      [2] =>  S
      [3] => ma
      [4] => sh
  )

Was This Helpful?

Previous Post
is_numeric()
Next Post
pathinfo()

0 Comments

Leave a Reply