Encodera

Thanks for reading!

Donate to the site — we work hard for you.

How to reverse a string

Short answer

For an ASCII string strrev() is enough:

<?php
 
echo strrev('hello');

Output:

olleh

Non-ASCII strings

strrev() reverses bytes and breaks multibyte characters. Split the string into characters with mb_str_split(), reverse the array and join it back:

<?php
 
echo implode('', array_reverse(mb_str_split('привет')));

Output:

тевирп

Functions used in this recipe

  • array_reverse — Returns an array with its elements in reverse order.
  • implode — Joins array elements into a string.

Thanks for reading!

Donate to the site — we work hard for you.