@stof - unfortunately I don't know much about character encodings, so this is interesting for me to look into.  Would the use of mb_strlen() here in place of strlen() be sufficient to correct this?