Categories: PHP

str_split – PHP String Functions

Syntax :

str_split ( string, length );

Description :

str_split() function will breaks a string into an array.


Parameter :

  • string – This is a Required parameter. It is the input string on which function will be performed.
  • length – This is an Optional parameter. It sets the length of each array element. Default length value is 1.

Output :

This will return an array of string.  it also depends on the length parameter value if,

  • length < 1, It will return FALSE.
  • length > larger than the length of string, the entire string will be returned as the only element of the array.

Related articles : explode(), count_chars(), str_word_count(), count_chars(), chunk_split().


str_split() – PHP Functions Example 1 : length value is 1 by default.
<?php
$strExample = "Hi from tutorialmines."; 
print_r(str_split($strExample));
?>

In above example ,We have a string “Hi from tutorialmines.”. Now this function will breaks the string into array of length 1. Output of above code in the browser is as below:

Array ( [0] => H [1] => i [2] => [3] => f [4] => r [5] => o [6] => m [7] => [8] => t [9] => u [10] => t [11] => o [12] => r [13] => i [14] => a [15] => l [16] => m [17] => i [18] => n [19] => e [20] => s [21] => . )

str_split() – PHP Functions Example 2 : length value is set to 4.
<?php
$strExample = "Hi from tutorialmines."; 
print_r(str_split ($strExample,4));
?>

In above example ,We have a string “Hi from tutorialmines.”. Now this function will breaks the string into array of length 4. Output of above code in the browser is as below:

Array ( [0] => Hi f [1] => rom [2] => tuto [3] => rial [4] => mine [5] => s. )

str_split() – PHP Functions Example 3 : Padding string “!” on BOTH side of string.
<?php
$strExample = "Hi from tutorialmines."; 
print_r(str_split($strExample,30));
?>

In above example ,We have a string “Hi from tutorialmines.”. Now this function will breaks the string into array of length 30. As length 30 is greater than the length of string. So, only one array element will be returned. Output of above code in the browser is as below:

Array ( [0] => Hi from tutorialmines. )


str_split() – PHP Functions Example 4 : False value will be returned when length is set to negative values or less than 1.
<?php
$strExample = "Hi from tutorialmines."; 
var_dump(str_split($strExample,-1)); //length is set with negative number.
echo "<br/>";
$strExample = "Hi from tutorialmines."; 
var_dump(str_split($strExample,0)); //length is set with zero 0.
?>

In above example ,We have a string “Hi from tutorialmines.”. Now this function will breaks the string into array of length -1 or 0. But this will retuen false value, as it support values greater than or equal to 1. Output of above code in the browser is as below:

bool(false)
bool(false)

jyoti rani

Recent Posts

Modern Toys, Magical Moments: Why the Best Toy Shop in Noida Is More Than Just a Store

When it comes to children, there’s one universal truth: the right toy can spark imagination, build skills, and make memories…

6 months ago

Rediscovering Joy: A New Era of Creativity & Comfort in Toy Stores

In today’s digital age, where screens and gadgets dominate our children’s lives, there’s something heartwarming about a well-loved plush toy…

6 months ago

Unboxing Imagination: Discovering the Joy of Play at a Toy Store in Noida

In a world dominated by screens and fast-paced routines, it’s easy to forget the simple magic of a toy in…

6 months ago

Imagination Unboxed: Discover Joy at the Toy Shop in Delhi

In the heart of Delhi’s vibrant streets lies a world where imagination meets innovation — the magical universe of toys.…

6 months ago

Play with Purpose: Discovering the Ultimate Toy Store in Noida

When was the last time a toy truly amazed you—not just as a product, but as a thoughtful tool for…

6 months ago

From Tears to Toys: Exploring Modern Childhood through Delhi’s Favorite Toy Shop

In the digital age, the way we experience childhood has changed, but the essence remains the same—imagination, exploration, and joy.…

6 months ago