Check encoding before applying htmlentities

Fix for #48
Merge pull request #49 from barryvdh/patch-1

Check encoding before applying htmlentities