Check pos variable before using substr() function