Replaced substr with mb_substr for UTF8 support