How to reverse a string
Short answer
For an ASCII string strrev() is enough:
<?php
echo strrev('hello');Output:
ollehNon-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.