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

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • HTML
    • JavaScript
    • jQuery
    • PHP
    • Python
    • SAS
    • Ruby
  • About
You are here: Home / PHP / php str_split() – Convert String into Array of Equal Length Substrings

php str_split() – Convert String into Array of Equal Length Substrings

March 29, 2022 Leave a Comment

The php str_split() function allows us to convert a string into an array of equal length substrings. By default, str_split() splits a string into an array of the string’s characters.

$variable = "I'm a variable";

print_r(str_split($variable));

//Output:
Array
(
    [0] => I
    [1] => '
    [2] => m
    [3] =>  
    [4] => a
    [5] =>  
    [6] => v
    [7] => a
    [8] => r
    [9] => i
    [10] => a
    [11] => b
    [12] => l
    [13] => e
)

You can also split a string into an array of equal length substrings of length n by passing a second argument to str_split().

$variable = "I'm a variable";

print_r(str_split($variable, 3));

//Output:
Array
(
    [0] => I'm
    [1] =>  a 
    [2] => var
    [3] => iab
    [4] => le
)

When working with string variables in our php programs, it is useful to be able to easily manipulate and change the value of the variables.

One such manipulation is being able to convert a string into an array of smaller strings.

The php str_split() function allows us the ability to convert a string into an array of substrings.

By default, str_split() splits a string into an array of the string’s characters.

Below is an example of using str_split() to convert a string into an array of the string’s characters.

$variable = "I'm a variable";

print_r(str_split($variable));

//Output:
Array
(
    [0] => I
    [1] => '
    [2] => m
    [3] =>  
    [4] => a
    [5] =>  
    [6] => v
    [7] => a
    [8] => r
    [9] => i
    [10] => a
    [11] => b
    [12] => l
    [13] => e
)

You can also split a string into an array of equal length substrings of length n by passing a second argument to str_split().

Below is a simple example of using str_split() and splitting a string into substrings of length 3.

$variable = "I'm a variable";

print_r(str_split($variable, 3));

//Output:
Array
(
    [0] => I'm
    [1] =>  a 
    [2] => var
    [3] => iab
    [4] => le
)

How to Loop Over Characters in String with str_split() Function in php

One example of how you can use the str_split() function is if you want to loop over all characters of a string variable.

We can first use str_split() to convert the string into an array of the string’s characters, and then loop over the array.

Below is an example of how to loop over the characters of a string in php.

$variable = "I'm a variable";

$chars = str_split($variable);

foreach($chars as $char) {
   echo $char . " ";
}

// Output:
I ' m   a   v a r i a b l e 

Hopefully this article has been useful for you to learn how to use the php str_split() function to convert a string into an array.

Other Articles You'll Also Like:

  • 1.  php Delete Files with php Unlink Function
  • 2.  php asinh – Find Hyperbolic Arcsine of Number Using php asinh() Function
  • 3.  Get Last Day of Month in php
  • 4.  echo php – Output One or More Strings
  • 5.  php rtrim() Function – Remove Whitespace at End of String
  • 6.  php array_splice() – Remove, Insert, and Replace Elements in Array
  • 7.  php is_string() Function – Check if Variable is String in php
  • 8.  php cosh – Find Hyperbolic Cosine of Number Using php cosh() Function
  • 9.  php Random Number Generator with rand Function
  • 10.  php is_array() Function – Check if Variable is Array in php

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy