Split a String by Letters with PHP

Every once in a blue moon you find yourself needing to take a string and converting it to an array of characters in the string. Maybe it’s because you want to count how often certain letters exist in the string or want to convert the letters to something else. To do this all you need in str_split(). Pass it the string without any additional arguments and you’re good to go:

$letters = str_split('Hello PHPAve.com Readers!!~!'

Additionally, if you wanted to split the string every 2 (or more) characters, you can pass a second argument to determine the length.

$chunks = str_split('Lorem ipsum ... dolor sit amet', 16

Have you used these functions before? I’d love to hear about how you used them!

Josh Sherman - The Man, The Myth, The Avatar

About Josh

Husband. Father. Pug dad. Musician. Founder of Holiday API, Head of Engineering and Emoji Specialist at Mailshake, and author of the best damn Lorem Ipsum Library for PHP.

If you found this article helpful, please consider buying me a coffee.