Talking about php string reverse interviews, talking about strings
1. single-byte string Inversion
Php provides the strrev () function for string inversion ()
$ Str = 'abcdef'; echo strrev ($ str );
2. mb_substr () is required for multi-byte strings that contain Chinese characters ()
$ Str = 'string'; function rev ($ str, $ encoding = 'utf-8') {$ len = mb_strlen ($ str); $ result = ''; for ($ I = $ len-1; $ I >=0; $ I --) {$ result. = mb_substr ($ str, $ I, 1, $ encoding);} return $ result;} echo rev ($ str );
3. algorithm-based first-order Switching
$ Str = 'abcdefg'; $ len = strlen ($ str); $ times = $ len/2; for ($ I = 0; $ I <= $ times; $ I ++) {$ tmp = $ str [$ I]; $ str [$ I] = $ str [$ len-$ I-1]; $ str [$ len-$ I-1] = $ tmp;} echo $ str;
In the above discussion, php string reversal interviews often involve all the content shared by xiaobian. I hope to give you a reference and support for the customer's house.