Below is the code that can be used to remove non ascii character from a string.
$cleanString = preg_replace(‘/[^(\x20-\x7F)]*/’,”, $dirtyString);
But what the hell that code means!
Well, that code uses regular expression to replace every single character that is not in the range of x20 – x7F in ASCII code with an empty character. In other words, it will delete character that is not in the ascii range x20 – x7F (Hexadecimal).
For more information about ASCII character code see the ASCII table or here. From the ASCII table you may see that x20 represents ‘Space’ and x7F represents ‘DEL’.
If you want to learn moreĀ about regular expression here is the link: http://www.regular-expressions.info
Cause we are using regular expression, theĀ code above can also be applied to any other platform (not only PHP) that supports regular expression.
