Use multibyte substr and strlen functions

Fixes spans being positioned incorrectly after multibyte characters