should be ``utf8``, not ``$encoding``, as you are working on the string converted to utf8, not on the string on the original encoding