Logo

Programming-Idioms

History of Idiom 41 > diff from v36 to v37

Edit summary for version 37 by programming-idioms.org:
[PHP] No sample value inside the snippet
↷

Version 36

2018-01-03, 23:26:00

Version 37

2018-01-03, 23:27:18

Idiom #41 Reverse a string

Create string t containing the same characters as string s, in reverse order.
Original string s must remain unaltered. Each character must be handled correctly regardless its number of bytes in memory.

Illustration

Idiom #41 Reverse a string

Create string t containing the same characters as string s, in reverse order.
Original string s must remain unaltered. Each character must be handled correctly regardless its number of bytes in memory.

Illustration
Code
$s = "lorém ipsüm dolör sit amor ❤ ";

for ($i=0;$i<mb_strlen($s);$i++) {

    $characters[] = mb_substr($s, $i, 1, 'UTF-8');

}

$characters = array_reverse($characters);
$t = implode($characters);
Code
for ($i=0;$i<mb_strlen($s);$i++) {
    $characters[] = mb_substr($s, $i, 1, 'UTF-8');
}

$characters = array_reverse($characters);
$t = implode($characters);
Comments bubble
This solution needs mb (multibyte) extension.
Comments bubble
This solution needs mb (multibyte) extension.