Logo

Programming-Idioms

History of Idiom 41 > diff from v20 to v21

Edit summary for version 21 by :
[PHP]Comment style
↷

Version 20

2015-12-18, 03:06:54

Version 21

2015-12-29, 23:23: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.

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.

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
$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);
Comments bubble
/* this solution needs mb (multibyte) extension */
Comments bubble
This solution needs mb (multibyte) extension.