Ad space (banner)
๐Ÿ˜PHP Lessons
Lesson 43 / 68

Reversing a String and Checking for Palindromes

In this lesson you will learn to reverse a string with mb_str_split and check for palindromes, while seeing how to handle multi-byte characters correctly. This is for anyone searching for "PHP reverse multibyte string".

To handle characters correctly - including accented letters and emoji - split the string into individual characters with mb_str_split() and reverse the order with array_reverse(). A string that reads the same forwards and backwards is called a palindrome.

The sample code splits the string character by character with mb_str_split($str), reverses the order with array_reverse(), and joins it back with implode('', ...). "racecar" comes out the same either way, so it is reported as a palindrome.

A common stumbling block for beginners is that plain str_split() does not split multi-byte characters correctly. Knowing the mb_ family of functions is essential background for any PHP application that handles non-ASCII text.

Unlike languages that treat a string as an array of characters, PHP expects you to reach for the dedicated multi-byte functions. Understanding that difference matters a great deal when building an international web service.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run PHP directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)