Split a String by Letters with PHP

Josh Sherman
1 min read
Software Development 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!

Join the Conversation

Good stuff? Want more?

Weekly emails about technology, development, and sometimes sauerkraut.

100% Fresh, Grade A Content, Never Spam.

About Josh

Husband. Father. Pug dad. Born again Linux user. Founder of Holiday API, Backend Engineering Manager and Emoji Specialist at Mailshake (WE'RE HIRING!), and author of the best damn Lorem Ipsum Library for PHP.

Currently Reading

Pears and Perils
Antifragile: Things That Gain from Disorder
Code Girls: The Untold Story of the American Women Code Breakers of World War II

Previous Reads

Buy Me a Coffee Become a Sponsor

Related Articles